Displaying Store Name in Magento CMS Pages and Static Blocks
As a senior developer at Laravel Company, I often encounter questions from our clients about customizing Magento stores. Today, let’s tackle the common challenge of displaying the store name within CMS pages and static blocks.
Understanding Magento Variables
Magento uses a templating engine called Twig to render content in CMS pages and static blocks. This templating system allows us to use variables and helper functions to dynamically insert data into our text.
The variable {{store url=""}} is an example of this, where it inserts the URL of the currently viewed store. However, as you’ve discovered, there isn’t a built-in equivalent for displaying the store name ({{store name=""}}).
Displaying Store Name without PHP File Access
Even if you don’t have direct access to the PHP files, you can still achieve this by using Magento’s helper functions within your CMS content.
Using the {{block type=""}} tag
You can use the block type="" tag to render content from a specific block. To display the store name, you can utilize the Magento_Theme/Block/Html/Title block.
Here is an example:
{% if block.type(block_type_id="Magento_Theme::html/title") %}
{{ block.html_title }} - {{ block.store.getName }}
{% endif %}
In this code:
- We’re checking if the
Magento_Theme::html/titleblock type exists. - We’re using the
block.html_titleto get the current page title. - The
block.store.getNamecalls the store model and retrieves the store name.
Displaying Store Name with PHP File Access
If you have access to request changes to the PHP files, here’s what you need to do:
- Open your theme’s
Magento_Theme/Block/Html/Title.phpfile. - Locate the
getName()method and add this line of code just before the return statement:
return $this->storeManager->getStore()->getName();
This will populate the store name variable which you can then access from your CMS content using the method mentioned above.
Conclusion
Displaying the store name in Magento CMS pages and static blocks is possible even if you don’t have direct PHP file access. By utilizing Magento’s block rendering capabilities and helper functions, you can dynamically insert the store name into your text.
Remember to always be cautious when making changes to live stores and consider testing these modifications on a staging environment first.
At Laravel Company, we specialize in providing comprehensive Magento solutions for businesses of all sizes. If you need expert guidance or assistance with customizing your e-commerce platform, feel free to reach out to our team through our contact page.
Happy developing!