I want to replace the image Alt in category page (list of products) with a variable, I don't want to keep the product label that Magento use by default, I want to get the value of MetaKeywords of the current Category.
My goal is to apply a single image alt value to all the product of the current category.
We are using a custom template in Magento 2.
I went to this file "image_with_borders.phtml" I tried to replace the value "getLabel" with "getMetaKeywords" but doesn't work.
Here how the code looks in that file:
alt="<?= $escaper->escapeHtmlAttr($block->getLabel()) ?>"/>
To replace with :
alt="<?= $escaper->escapeHtmlAttr($category->getMetaKeywords()) ?>"/>
Hello @eminemusah16c9
In Magento 2, you can get the value of the meta keywords for the current category using the following steps:
protected $_pageConfig; public function __construct( ... \Magento\Framework\View\Page\Config $_pageConfig, ... ) { ... $this->_pageConfig = $_pageConfig; ... }
$metaKeywords = $this->_pageConfig->getKeywords();
protected $_request; public function __construct( ... \Magento\Framework\App\Request\Http $_request, ... ) { ... $this->_request = $_request; ... } public function getMetaKeywords() { if ($this->_request->isCategory()) { $metaKeywords = $this->_pageConfig->getKeywords(); // Do something with $metaKeywords } }
That's it! Now you have the MetaKeywords value for the current category in the $metaKeywords variable, and you can use it as needed in your Magento 2 code.
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9