2

Tried adding {{store}} and {{store_name}} in the Mask for Meta Title to output the site name on each product in the Product Fields Auto-Generation. Any idea if possible or what the correct store name variable is? enter image description here

2

Unfortunately auto-generation fields doesn't allow to use store name for mask. And store name could be a problem to generate product meta title in this way because every product could be assigned to the website.

Furthermore I do not think you need this way to generate product title. Here is way would be more useful I think..

Create new module

app/code/Vendor/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

app/code/Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="2.1.0"/>
</config>

you need to define new event

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_load_after">
        <observer name="vendor_product_load_after" instance="Vendor\Module\Observer\AfterProductLoad" />
    </event>
</config>

and add event handler app/code/Vendor/Module/Observer/AfterProductLoad.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Store\Model\StoreManagerInterface;

class AfterProductLoad implements ObserverInterface
{
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        StoreManagerInterface $storeManager
    ){
        $this->storeManager = $storeManager;
    }

    public function execute(EventObserver $observer)
    {
        $product = $observer->getProduct();
        if ($product->getIsMetaApplied()) {
            return;
        }
        if ($metaTitle = $product->getMetaTitle()) {
            $metaTitle .= ' | ' . $this->storeManager->getStore()->getName();
            $product->setMetaTitle($metaTitle);
            $product->setIsMetaApplied(true);
        }
    }
}

do not forget to upgrade system

php bin/magento setup:upgrade

Hope this helps.

| improve this answer | |
  • This will be set product meta title in database but how can we get controller level same thing ? – Manish Maheshwari Dec 23 '19 at 10:31
0

@Nicholas Miller's answer worked for me but saving products in the admin area resulted in Admin being used as the site name instead. I used

public function getStoreName()
{
    return $this->_scopeConfig->getValue(
        'general/store_information/name',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

to get the store name instead of

$this->storeManager->getStore()->getName();

Also, this appends the store name repeatedly so I used explode, implode and array_pop to replace, rather than append to the string, the store's name in the meta title.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.