1

CURRENT STATE:

Custom theme installed

Title of the product in the product details page displays the product name

In backend: Made new custom attribute (brand), put it to default attribute set, added some values

DESIRED STATE:

Product title on the product details page displays BRAND + NAME


I tried changing title.phtml from vendor > magento > module-theme > view > frontend > templates > html > title.phtml but I cannot get the value of custom attribute no matter how I try (I can only set some static value instead of $title but no real/dynamic values for each product).

Thanks in advance for any help

  • did you get any solution ? – Pawan Aug 12 '18 at 7:10
0

I have resolved your issue by creating a frontend plugin

<?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\Theme\Block\Html\Title">
        <plugin name="product_title" type="Mbs\Title\Plugin\ProductTitleWithBrand" />
    </type>
</config>

and then the plugin reads whether the page title to render is for a product registered in the registry as per product detail view.

class ProductTitleWithBrand
{
    /**
     * @var \Magento\Framework\Registry
     */
    private $_coreRegistry;

    private static $attributePrefix = 'brand';

    public function __construct(
        \Magento\Framework\Registry $_coreRegistry
    ) {
        $this->_coreRegistry = $_coreRegistry;
    }

    public function afterGetPageHeading(\Magento\Theme\Block\Html\Title $subject, $title)
    {
        if ($this->getProduct()) {
            $prefix = $this->getProduct()->getAttributeText(self::$attributePrefix);
            if ($prefix) {
                $title = $prefix.' '.$title;
            }
        }

        return $title;
    }

    /**
     * Retrieve currently viewed product object
     *
     * @return \Magento\Catalog\Model\Product
     */
    private function getProduct()
    {
        return $this->_coreRegistry->registry('product');
    }
}

see full code at https://bitbucket.org/magstaging/mbstitle

  • Oh wow, thank you very much Herve, it works! I'd give you 100 upvotes if I could :) Cheers! – web player Aug 13 '18 at 10:03

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.