How to Create And Add Custom Magento Variables in Magento 2

How to Create And Add Custom Magento Variables in Magento 2

Are you looking to enhance your Magento 2 store with personalized content and features? Custom Magento variables are key to achieving this. They allow you to manage dynamic content efficiently. You can update it centrally without needing to alter individual pages or templates.

This tutorial will explain how to create and add custom variables in Magento 2.

Key Takeaways

  • Learn how to create custom variables in Magento 2.

  • Understand how to add these variables to your store's email templates and pages.

  • Discover how custom variables can streamline content updates across your site.

  • Gain insight into enhancing your store's functionality and personalization with custom variables.

What are Custom Variables?

Custom Magento variables are placeholders that store specific data. They allow for centralized management of information within a system. It enables a uniform display of data across many pages or sections of a website. This feature is useful for data that needs frequent updates, such as,

  • Contact information

  • Promotional messages

  • Operational notices

By updating the variable's value in one location, all instances where the variable is used will automatically show the changes. It ensures consistency and accuracy of information throughout the website. It also simplifies the process of maintaining the site.

Benefits of Custom Variables

1. Centralized Content Management

Custom variables enable centralized updates of essential information. It includes:

  • Contact details

  • Promotional content

For example, if a retailer updates their contact number in one centralized location, this change will automatically update. It will update all pages that display the contact details. It eliminates the need for individual page updates.

2. Consistency Across Pages

By using custom variables, information remains uniform across all website pages. This consistency is beneficial in maintaining a consistent brand voice and user experience. For example, customers will receive consistent information on product descriptions and pricing across all pages they visit.

3. Time and Effort Savings

These variables reduce the need for updating information across many pages individually. During promotions, updating the content of a promotional banner in one central place ensures automatic updates. This change appears across all pages where the banner is displayed. It eliminates the manual effort of editing many templates or sections.

4. Personalization and Relevance

Custom variables can display tailored messages or offers. These are based on user behavior or preferences. It leads to a more personalized shopping experience. For example, showing a special discount code to returning customers on their home pages or product pages enhances engagement. It also increases conversion rates.

5. Error Reduction

By centralizing updates, the risk of discrepancies and errors across different parts of the site is minimized. This approach helps avoid issues such as outdated or conflicting information. Such issues can confuse customers and harm the brand's credibility.

6. Flexibility in Content Deployment

Custom variables offer,

  • Flexibility

  • Easy integration into various templates

  • Emails

It simplifies content management and deployment across different channels. For example, in email marketing, custom variables can personalize email content. They tailor greetings or special offers based on the customer's past purchases or browsing history. It ensures that each communication is relevant and engaging.

6 Steps to Create Custom Variables in Magento 2

Step 1: Log Into the Admin Panel

Log Into The Admin Panel

Start by signing into the backend of your Magento 2 store.

Step 2: Navigate to Custom Variables

Navigate to Custom Variables

  • Navigate to the System menu.

  • Select Custom Variables under the Other Settings submenu.

  • Access the page where you can manage your custom variables.

Step 3: Add a New Variable

Add a New Variable

Click on the Add New Variable button. You will be directed to a form where you can create your new variable.

Step 4: Configure the Variable

Configure the Variable

  • Variable Code: Enter a unique code for the variable. This code is used in templates to reference the variable.

  • Variable Name: Provide a name for the variable that helps identify its purpose.

  • Variable Value: Input the information or data you wish to store. It could be text, numbers, or HTML content.

Step 5: Save Your Change

Save Changes

After filling out the form, click the Save button to store your new variable.

Step 6: Implement the Variable

To use the variable on your site, insert the variable code into your store’s templates. Place it where you need the information to appear.

Which Variables Can Be Used in Templates?

Category Variable Description
Base URL {{base url}} The base URL of your store.
{{store url="path/to/store"}} URL for a specific store view or path.
Store Information {{store name}} The name of the store.
{{store front name}} The front name of the store.
{{store phone}} The store's phone number.
{{store hours}} The operating hours of the store.
Customer Information {{customer name}} The full name of the customer.
{{customer email}} The email address of the customer.
Product Information {{product name}} The name of the product.
{{product price}} The price of the product.
{{product sku}} The SKU of the product.
Order Information {{order number}} The order ID or number.
{{order date}} The date the order was placed.
{{order status}} The current status of the order.
{{order total}} The total amount of the order.

How do You Add Custom Variables to an Email Template in Magento 2?

Step 1: Edit the Email Template

  • Access Marketing > Communications > Email Templates from the main menu.

Access Email Template

  • Either create a new template by selecting Add New Template or edit an existing one by clicking on its name.

Create Or Add New Template

  • In the template content area, insert your custom variable. Use the format \{{customVar code=your_variable_code}}\ Replace \your_variable_code\ with the code you assigned when creating the variable.

Insert Custom Variable in Template Content Area

Step 2: Save Changes

Sve Changes

Once you have inserted the custom variable into the email template, click on Save Template. This action will apply your changes.

FAQs

1. What are custom variables in Magento 2?

Custom variables in Magento 2 let you store dynamic content. This content can include values, text, or HTML code. You can reuse it throughout your store.


2. How can I add custom variables in Magento 2?

To add custom variables in Magento 2, follow these steps: First, navigate to the admin panel. Then, go to Content > Elements > Blocks. Finally, click on Add New Block. You can then define the variable name and value to be used in your store.


3. What is the process for inserting custom variables in Magento 2?

To insert custom variables in Magento 2, use the syntax {{custom_variable_code}}. Apply this within the content of a page, block, or email template. It will display the custom value where needed.


4. How do I use custom variables in Magento 2?

You can use custom variables in Magento 2 in several ways. Reference them in CMS pages, static blocks, product descriptions, email templates. It allows for easy management and updating of content across your store.


5. Can I create predefined and custom variables in Magento 2?

Magento 2 supports the creation of both predefined variables, provided by the system, and custom variables. You can define these custom variables based on your specific needs.


6. Where can I see the variable reference for a list of allowed variables in Magento 2?

You can find a list of variables that are allowed in Magento 2 by consulting the official Magento user guide. This documentation is available on the Magento website.


7. What is the significance of using custom variables in Adobe Commerce?

Using custom variables in Adobe Commerce enhances personalized and dynamic content across your store. It improves the user experience and adds flexibility to content management.

Summary

Magento variables are a powerful tool. They efficiently manage and update dynamic content across your website. By following these steps, you can achieve

  1. Significant enhancements with custom variables in Magento 2. These variables enable easy updates and ensure consistency throughout the platform.

  2. They also offer easy personalization and integration into both website and email templates.

  3. Custom variables improve management and store responsiveness.

Explore your store’s potential with Magento server hosting to enhance the advantage of magento variables.

CTA

Andrea Oriane
Andrea Oriane
Technical Writer

Andrea specializes in creating informative content for Magento. With extensive e-commerce knowledge and understanding of Magento functionalities, she crafts articles for a wide range of audience.


Get the fastest Magento Hosting! Get Started