1

turncate Meta title and meta description on pdp page how we can do that in which file we need to make changes .

  • what you mean by truncate? – Amit Bera Jul 15 at 6:44
  • i want turncate length – imtiazau Jul 15 at 6:48
  • Meta Title Case 1: When the length of the Product Title is exceeding 60 Characters including the white spaces. Then Truncate it up to 60 Characters and use as “Meta Title” Proposed Meta Title : “{Product Title}” (But Truncated up to 60 Characters Only) Truncate the length of Product Title up to 60 Characters and use them for Meta Title of Product level pages. – imtiazau Jul 15 at 6:48
  • i suggest to create custom script and than replace title and description. first get title and count word length and then truncate . one more thing why only in pdp page ? – Anas Mansuri Jul 15 at 7:28
  • for seo on ogogle – imtiazau Jul 15 at 7:32
1

In PDP, meta title and description is set via the product view helper class: \Magento\Catalog\Helper\Product\View::preparePageMetadata().

So what you need to do is write a preference for this helper class as the method preparePageMetadata() is a private method. But, the best option would be doing the same through a plugin. To do this, you need to write an around plugin for the method \Magento\Catalog\Helper\Product\View:: prepareAndRender() which is what actually call the private method preparePageMetadata().

So I will just show you how you can achieve this through a plugin.

File: app/code/[Namespace]/[Module]/etc/frontend/di.xml

<?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\Helper\Product\View">
        <plugin
                name="NamespaceModuleAlterPDPMetaTags"
                type="Namespace\Module\Plugin\Catalog\Helper\Product\ViewPlugin" />
    </type>
</config>

Basically, this file will register our plugin dependency inside our module Namespace_Module.

File: app/code/[Namespace]/[Module]/Plugin/Catalog/Helper/Product/ViewPlugin.php

<?php

namespace Namespace\Module\Plugin\Catalog\Helper\Product;

use Magento\Catalog\Helper\Product\View as ProductViewHelper;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Page;
use Magento\Framework\Stdlib\StringUtils;


class ViewPlugin
{
    /**
     * @var \Magento\Framework\Registry $coreRegistry,
     */
    private $coreRegistry;

    private $resultPage;

    private $string;

    public function __construct(Registry $coreRegistry, StringUtils $string)
    {
        $this->coreRegistry = $coreRegistry;
        $this->string = $string;
    }

    public function aroundPrepareAndRender(
        ProductViewHelper $productViewHelper,
        callable $proceed,
        Page $resultPage,
        $productId,
        $controller,
        $params = null
    ) {
        // collecting result page instance for future use
        $this->resultPage = $resultPage;

        // send the control to the original method
        $result = $proceed($resultPage, $productId, $controller, $params);

        // after meta title and description is generated, this will be executed

        // make sure the helper instance is correct
        if (!$result instanceof ProductViewHelper) {
            return $productViewHelper;
        }

        // collecting product information from the core registry
        $product = $this->coreRegistry->registry('product');

        // changing meta title and description as needed
        $this->alterMetaTitle($product);
        $this->alterMetaDescription($product);

       return $result;
    }

    /**
     * Changing meta title of PDP; Allowing only 60 characters
     * @param $product
     */
    private function alterMetaTitle($product)
    {
        $pageConfig = $this->resultPage->getConfig();
        $title = $product->getMetaTitle();

        if ($title) {
            $pageConfig->getTitle()->set($this->string->substr($title, 0, 60));
        }
    }

    /**
     * Changing meta description of PDP; Allowing only 60 characters
     * @param $product
     */
    private function alterMetaDescription($product)
    {
        $pageConfig = $this->resultPage->getConfig();
        $description = $product->getMetaDescription();

        if ($description) {
            $pageConfig->setDescription($this->string->substr($description, 0, 60));
        } else {
            $pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 60));
        }
    }
}

As you can see, we wrote an around plugin for the method prepareAndRender(). Why we are relying on an around plugin is because we need the parameter $resultPage of the method in order to alter the meta title and description. Also, we need $product instance in order to retrieve the meta title and description. Product initialization is also happening inside prepareAndRender() method and hence will be obtained through the registry.

You can customize the methods alterMetaTitle() and alterMetaDescription() as per your need. Currently, it will enforce 60 character limit to both meta title and description.

I hope that gives you an idea.

Note: Module creation and usage of the plugin is out of scope concepts as per this question. If you need to know more details, please refer devdocs.

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.