Skip to content

Magento 2.3.5-p2: Show different title and alt for product img based on store

I’m trying to show a different Title and Alt depending on the store for the product page image.

For example on a english store (store id 1) xyz.com/uk/ the Title and Alt would be

title=”English Title”
alt=”English Alt”

On a Japanese store (store id 2) xyz.com/jp/ the Title and Alt would be

title=”Japanese Title”
alt=”Japanese Alt”

On a Danish store (store id 3) xyz.com/dk/ the Title and Alt would be

title=”Danish Title”
alt=”Danish Alt”

The code I’m using is as follows, this doesn’t change the title and alt depending on the store.

Location: app/design/frontend/Vendor/theme/Magento_Catalog/templates/product/view/gallery.phtml

<?php

$product = $block->getProduct();
$imageHelper = $this->helper('MagentoCatalogHelperImage');

if (!$product->getImage() || $product->getImage() == 'no_selection') {
    $image = $imageHelper->getDefaultPlaceholderUrl('image');
} else {
    $image = $imageHelper->init($product, 'product_page_image_medium')
            ->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
            ->setImageFile($product->getImage())
            ->getUrl();
}
?>
<div class="gallery-placeholder">
    <img src="<?php echo $image; ?>" alt="English Alt" title="English Title" >
</div>

I found a possible solution on getting the current store ID based on this article.

$objectManager =  MagentoFrameworkAppObjectManager::getInstance();        
 
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
 
$storeManager->getStore()->getStoreId();

But I’m not able to put the above code together to do a what I assume is a if/else statement to show the title and alt based on the store.