1

I have made a module to override Magento\Catalog\Block\Product\View. I want to add some standard things to the MetaTitle and MetaDescription on the product page. Specifically for empty descriptions.

Alters

I alter the title string and save with

$this->pageConfig->setTitle($myString).

The same goes for the description

$this->pageConfig->setDescription($myOtherString)

Problem

The MetaTitle and MetaDescription on the product page stay the same.

Possible causes and solutions

I have investigated and ruled out the following:

  • Cache
  • Third party modules
  • Module related problems (magento setup:upgrade,...)

Debug

When I echo the string I want to set I get the following error:

a:4:{i:0;s:282:"Warning: Cannot modify header information - headers already sent by (output started at /app/code/[MyVendor]/[MyModule]/Block/Product/View.php:61) in /www/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php on line 125";i:1;s:1702:"#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(2, 'Cannot modify h...', '/data/sites/web...', 125, Array)
#1 /www/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php(125): setcookie('mage-messages', '[]', 1533729078, '/', '', false, false)
#2 /www/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php(98): Magento\Framework\Stdlib\Cookie\PhpCookieManager->setCookie('mage-messages', '[]', Array)
#3 /www/vendor/magento/module-theme/Controller/Result/MessagePlugin.php(85): Magento\Framework\Stdlib\Cookie\PhpCookieManager->setPublicCookie('mage-messages', '[]', Object(Magento\Framework\Stdlib\Cookie\PublicCookieMetadata))
#4 /www/vendor/magento/framework/Interception/Interceptor.php(152): Magento\Theme\Controller\Result\MessagePlugin->afterRenderResult(Object(Magento\Framework\View\Result\Page\Interceptor), Object(Magento\Framework\View\Result\Page\Interceptor))
#5 /www/var/generation/Magento/Framework/View/Result/Page/Interceptor.php(26): Magento\Framework\View\Result\Page\Interceptor->___callPlugins('renderResult', Array, Array)
#6 /www/vendor/magento/framework/App/Http.php(139): Magento\Framework\View\Result\Page\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
#7 /www/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#8 /www/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))

But I think this is more an issue of the echo than error due to code error.

I mainly followed this logic. Is there another logic I should follow or does anyone have an idea of why the metainfo does not change?

Thanks for your time!

3
  • You cant override View.php file you have to create plugin for your changes. Aug 8, 2017 at 12:16
  • Thanks Rakesh. Is it possible to get a specific example of a plugin for metadata? Aug 8, 2017 at 14:12
  • I've studied Plugins but are too limiting for my purposes. eg. I want to be able to call the current product. Plugins don't seem suitable to do that Aug 9, 2017 at 12:18

1 Answer 1

0
app/code/Vendor/Modulename/etc/di.xml

create di.xml file and called plugin function,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <type name="Magento\Catalog\Block\Product\View">
        <plugin name="block_product_view_plugin" type="Vendor\Modulename\Plugin\Productview"/>
    </type>
</config>

Create plugin file inside your module,

<?php
namespace Vendor\Modulename\Plugin;

use Closure;

class Productview
{
    public function aroundGetProduct( 
        \Magento\Catalog\Block\Product\View $subject,
        Closure $proceed
    )
    {       
        $newdata = $proceed();        

        if($newdata){
            $metaTitle = $newdata->getMetaTitle(); 
            $newdata->setMetaTitle($metaTitle);           
            return $newdata; 
        }
        return $newdata;
    }
}
1
  • Thanks for your effort. But this acts on the product itself. I want to change the metadescription according to the product page. It would be nice to include the current category in the metadescription Aug 10, 2017 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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