0

I am trying to set meta title tag after redirect. I am using the answer from this link.(Plz check the answer in tha above link which has been awarded a bounty of +50 reputations). I have also added that answer in my question.

Setting Meta Title with Redirect

My Code. I have created events.xml file in my module.

/app/code/Vendor/Module/etc/frontend/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="dynamic_title_generator" instance="Vendor\Module\Observer\AfterProductLoad" />
    </event> 
</config>

Add below code in my Observer

/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();

        //You can set any dynamic title from here as well.
        $product->setMetaTitle('custom-title');
        $product->setIsMetaApplied(true);
    }
}

The above code works fine and I am able to set the meta title data for the loaded product.

Now in the above observer I am adding a code that will load a product using ID and fetch its meta title attribute's data.

Updated observer with the following code:

<?php
namespace Matrid\ImprovedConfigurable\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class AfterProductLoad implements ObserverInterface
{
    /**
     * @var StoreManagerInterface
    */

    private $_storeManager;
    private $_productloader;

    /**
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\ProductFactory $_productloader
    ){
        $this->_storeManager = $storeManager;
        $this->_productloader = $_productloader;
    }



    public function execute(EventObserver $observer)
    {
        $product = $observer->getProduct();
    
        //Below 3 lines were added
        $simpleProductId = '61';
        $simpleProduct = $this->_productloader->create()->load($simpleProductId);
        $metaTitle = $simpleProduct->getName();
    
        $product->setMetaTitle($metaTitle);
        $product->setIsMetaApplied(true);
    
    }
}

But now I am getting an error due to this line of code.

$simpleProduct = $this->_productloader->create()->load($simpleProductId);

And the error goes after removing the above line and page loads fine. This is the error screenshot enter image description here

Now I feel I am getting an error because of loading product object in the event observer.

But I need to load a product using id and use its attribute's data as meta title.

Now my question is:

  1. Is trying to load a product object in the above observer is wrong?

  2. And If yes, then how I can go on loading a product data and use that as meta title. Should I go with another event, I am not sure, Plz guid me regarding this.

Update: Following Michal Biarda suggestion solves the issue. Correct code that worked.

public function execute(EventObserver $observer)
{
        $product = $observer->getProduct();
    
        if(!$this->_processFlag){
            $this->_processFlag=true;
            $simpleProductId = 61;
            $simpleProduct = $this->_productloader->create()->load($simpleProductId);
        
            $metaTitle = $simpleProduct->getMetaTitle();
            $product->setMetaTitle($metaTitle);
            $product->setIsMetaApplied(true);
    }   
            
}
2
  • Checking the Magento and web server logs may give you more specific information about the error.
    – jiheison
    Commented Jun 6, 2021 at 17:10
  • I checked magento logs and did not find anything related to this. I can not check server logs as I do not have access to that. Will try to update my question if I can get access to server logs. But still removing this single line solves the issue and page loads fine. $simpleProduct = $this->_productloader->create()->load($simpleProductId); Only adding this line gives the error, I think this is something related to loading product object in this observer. Commented Jun 6, 2021 at 17:43

2 Answers 2

1

You are loading a product after product is loaded, making an infinite loop. You could prevent this error by setting some custom flag on product 61 and checking the flag at the beggining of your observer method.

Edit: Actually setting the flag on the product might be harder than it looks like. The easier solution would be to set a flag on the observer itself, ie. at first check flag, if it is null, update its value to true, and perform the loading of product 61.

9
  • I have found an answer according to your suggestion but it was for magento 1. Plz have a look and let me know if it same that you are suggesting. stackoverflow.com/questions/28348590/…. and if you could suggest a code for this in magento2 Commented Jun 6, 2021 at 17:56
  • I have found a different answer according to your suggestion, but this is also for magento 1. magento.stackexchange.com/questions/50244/…, plz have a look and suggest this for magento 2 Commented Jun 6, 2021 at 17:59
  • 1
    Good point! Is there any reason to load a new product ($simpleProduct) at all here?
    – jiheison
    Commented Jun 6, 2021 at 18:37
  • Yes I need to load a simple product data. This is the page url where I am trying to achieve this. magento-561260-1838674.cloudwaysapps.com/…. Actually here a configurable product is loaded and query is passed in the url to auto select a simple product. So I want to fetch that simple product's meta title data and assign here using the event. Commented Jun 6, 2021 at 19:27
  • In that case, this answer is a good approach, i.e. setting a flag on the product and checking to ensure that your observer isn't loading the product that triggered the observer in the first place.
    – jiheison
    Commented Jun 6, 2021 at 20:17
0

There is some issue with Magento if we are loading multiple products, I am not sure if it works this way. I'd suggest you to try Repository approach instead of Models, it has solved my issue. Add Product Repository in your constructor like below :

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepo,
        array $data = []
    ) {
        $this->_productRepo = $productRepo;
        parent::__construct( $context, $data );
    }

and then alternative of load() in this case would be :

        $product = $this->_productRepo->getById($productId);

You can do anything with this product that you can do with one loaded from models. I think it should work in your case.

1
  • No loading product using productrepository did not worked, same issue. My page goes in an infinite loop and I get a server error. Commented Jun 7, 2021 at 10:04

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.