2

catalog_controller_product_view event cannot be used in conjunction with FPC cache since Magento will not raise it after the first visit. That's why I am using controller_front_send_response_before observer as it is described in:
https://magento.stackexchange.com/a/126377/31910

My question is, how can I get current product id in this observer?

3 Answers 3

2

The main idea is use a request for your purposes. There is a two cases: path without an url rewrite and with url rewrite. Here is a sample code which parse path from request and obtain a product id:

<?php

namespace Vendor\Module\Observer;

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

/**
 * Class ResponseBefore
 *
 */
class ResponseBefore implements ObserverInterface
{
    /**
     * @var \Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product
     */
    private $productUrlRewriteResource;

    /**
     * ResponseBefore constructor.
     *
     * @param \Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product $productUrlRewriteResource
     */
    public function __construct(
        \Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product $productUrlRewriteResource
    ) {
        $this->productUrlRewriteResource = $productUrlRewriteResource;
    }

    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        /** @var \Magento\Framework\App\RequestInterface $request */
        $request = $observer->getRequest();

        // Check is we are on the product page

        if ($request->getParam('id')) {
            // Regular request like `catalog/product/view/id/8`
            $id = $request->getParam('id');
        } else {
            // In case url rewrite we should search id in the `url_rewrite` table by path and type
            /** @var string $pathInfo */
            $pathInfo         = $request->getPathInfo();
            $preparedPathInfo = ltrim(trim($pathInfo), "/");

            $connection = $this->productUrlRewriteResource->getConnection();
            $table      = $this->productUrlRewriteResource->getTable('url_rewrite');
            $select     = $connection->select();
            $select->from($table, ['entity_id'])
                   ->where('entity_type = :entity_type')
                   ->where('request_path LIKE :request_path');

            $result = $connection->fetchCol(
                $select,
                ['entity_type' => 'product', 'request_path' => $preparedPathInfo]
            );
            $id     = isset($result[0]) ? $result[0] : null;
        }

        // Do something here with parsed id

        return;
    }
}

It's not a complete code, but it should help you to write what a you want.

3
  • It worked. Thanks. After product cached using Full page cache, registry was not working, but from your code I am able to get product. :) Apr 8, 2019 at 13:40
  • @ShoaibMunir I'm glad to help You :) Apr 8, 2019 at 13:41
  • 1
    @ShoaibMunir be careful with that kind of code, because it can lead to errors in much cases, like enable store codes in url, custom url-rewrites etc. Apr 8, 2019 at 13:43
2

Istead of the observer you can use the following method to get the detail of current product

use Magento\Framework\Registry;

class yourclassname
{
    /**
     * @var Registry
     */

    protected $_registry;

    public function __construct(Registry $registry)
    {
            $this->_registry = $registry;
    }

    public yourfunction()
    {
        $currentProduct = $this->_registry->registry('current_product');
        //to get id
        $productId=$currentProduct->getId();
        //do rest of your work
    }
}
0
2

@mohith your code is fine if your using magento 2.2.x. Registry has been deprecated in Magento 2.3.x.

Use Magento/Catalog/Model/Session in Magento Latest version to get the current product.

1
  • Thanks @Venugopal :) Jul 9, 2019 at 10:00

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.