cancel
Showing results for 
Search instead for 
Did you mean: 

Replace Image Alt with variable in Magento 2

Replace Image Alt with variable in Magento 2

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()) ?>"/>
1 REPLY 1

Re: Replace Image Alt with variable in Magento 2

Hello @eminemusah16c9 

 

In Magento 2, you can get the value of the meta keywords for the current category using the following steps:

  • Step 1: Inject the \Magento\Framework\View\Page\Config class in your PHP file or block class. You can do this by adding the following code to your constructor:
    protected $_pageConfig;
    		public function __construct(
    			...
    			\Magento\Framework\View\Page\Config $_pageConfig,
    			...
    		) {
    			...
    			$this->_pageConfig = $_pageConfig;
    			...
    		}
  • Step 2: Use the $this->_pageConfig object to get the MetaKeywords value for the current category. You can do this by calling the getKeywords() method on the $this->_pageConfig object, like this:
    $metaKeywords = $this->_pageConfig->getKeywords();
  • Step 3: Make sure you are on a category page. You can use the \Magento\Framework\App\Request\Http class to check if you are currently on a category page. You can do this by adding the following code to your constructor and calling the isCategory() method on the $this->_request object:
    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