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:

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

3 Answers 3


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:


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(
                ['entity_type' => 'product', 'request_path' => $preparedPathInfo]
            $id     = isset($result[0]) ? $result[0] : null;

        // Do something here with parsed id


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

  • 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

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
        //do rest of your work

@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.

  • 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.