How to Add Product Name as Default Alt Text to All Product Images in Magento 2
Steps to Add Product Name as Default Alt Text to All Product Images in Magento 2
Hello Magento Friends,
Today I will be explaining How to Set Product Name as Default Alt Text to All Product Images in Magento 2.
Alt text for product images informs the search engine what the image is about and helps index the product image when the user fires a query related to it. For Magento 2 stores, it is essential to give alt text to all the product images to aid in search engine rankings. Large stores can take advantage of Magento 2 Image Alt Tags Extension to automate the product image alternate text process and save time.
You can also set the product name as the default alt text for all the product images in Magento 2. To do that, follow the below-given steps
Contents [hide]
1 Steps to Set Product Name as Default Alt Text to All Product Images in Magento 2:
2 Conclusion:
Steps to Set Product Name as Default Alt Text to All Product Images in Magento 2:
Step 1: First, we need to create a “di.xml” file inside our extension at the following path
app\code\Vendor\Extension\etc\frontend
Then add the code as follows
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Block\Product\View\Gallery">
<plugin name="alt_text_plugin" type="Vendor\Extension\Block\Product\View\Gallery\Plugin" sortOrder="10" />
</type>
</config>
Step 2: After that, we need to create a “ Plugin .php” file inside extension Controller directory
app\code\Vendor\Extension\Block\Product\View\Gallery\
Now add the code as mentioned below
<?php
namespace Vendor\Extension\Block\Product\View\Gallery;
use Vendor\Extension\Helper\Data;
use Magento\Catalog\Model\Layer\Resolver;
class Plugin
{
public function afterGetGalleryImages($block, $images)
{
try
{
$product = $block->getProduct();
$name=$product->getName();
foreach ($images as $image) {
$image->setLabel($name);
}
}
catch (Exception $e)
{
\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($e->getMessage());
}
return $images;
}
}
ArduinoDroidTechyMad
ReplyDelete