Get current product id in Observer controller_front_send_response_before





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















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?










share|improve this question































    0















    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?










    share|improve this question



























      0












      0








      0








      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?










      share|improve this question
















      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?







      magento2 event-observer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 15 hours ago







      Shoaib Munir

















      asked 16 hours ago









      Shoaib MunirShoaib Munir

      2,3692829




      2,3692829






















          2 Answers
          2






          active

          oldest

          votes


















          1














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



          use MagentoFrameworkRegistry;

          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
          }
          }





          share|improve this answer


























          • let me know if any issues

            – mohith
            10 hours ago











          • I tried this. It is that simple and it's working. Thanks

            – Shoaib Munir
            8 hours ago



















          1














          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 VendorModuleObserver;

          use MagentoFrameworkEventObserver;
          use MagentoFrameworkEventObserverInterface;

          /**
          * Class ResponseBefore
          *
          */
          class ResponseBefore implements ObserverInterface
          {
          /**
          * @var MagentoCatalogUrlRewriteModelResourceModelCategoryProduct
          */
          private $productUrlRewriteResource;

          /**
          * ResponseBefore constructor.
          *
          * @param MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          */
          public function __construct(
          MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          ) {
          $this->productUrlRewriteResource = $productUrlRewriteResource;
          }

          /**
          * @param Observer $observer
          * @return void
          */
          public function execute(Observer $observer)
          {
          /** @var MagentoFrameworkAppRequestInterface $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.






          share|improve this answer


























          • Thanks for the answer. +1

            – Shoaib Munir
            8 hours ago












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f268938%2fget-current-product-id-in-observer-controller-front-send-response-before%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














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



          use MagentoFrameworkRegistry;

          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
          }
          }





          share|improve this answer


























          • let me know if any issues

            – mohith
            10 hours ago











          • I tried this. It is that simple and it's working. Thanks

            – Shoaib Munir
            8 hours ago
















          1














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



          use MagentoFrameworkRegistry;

          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
          }
          }





          share|improve this answer


























          • let me know if any issues

            – mohith
            10 hours ago











          • I tried this. It is that simple and it's working. Thanks

            – Shoaib Munir
            8 hours ago














          1












          1








          1







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



          use MagentoFrameworkRegistry;

          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
          }
          }





          share|improve this answer















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



          use MagentoFrameworkRegistry;

          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
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 7 hours ago









          sv3n

          9,93662456




          9,93662456










          answered 10 hours ago









          mohithmohith

          713




          713













          • let me know if any issues

            – mohith
            10 hours ago











          • I tried this. It is that simple and it's working. Thanks

            – Shoaib Munir
            8 hours ago



















          • let me know if any issues

            – mohith
            10 hours ago











          • I tried this. It is that simple and it's working. Thanks

            – Shoaib Munir
            8 hours ago

















          let me know if any issues

          – mohith
          10 hours ago





          let me know if any issues

          – mohith
          10 hours ago













          I tried this. It is that simple and it's working. Thanks

          – Shoaib Munir
          8 hours ago





          I tried this. It is that simple and it's working. Thanks

          – Shoaib Munir
          8 hours ago













          1














          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 VendorModuleObserver;

          use MagentoFrameworkEventObserver;
          use MagentoFrameworkEventObserverInterface;

          /**
          * Class ResponseBefore
          *
          */
          class ResponseBefore implements ObserverInterface
          {
          /**
          * @var MagentoCatalogUrlRewriteModelResourceModelCategoryProduct
          */
          private $productUrlRewriteResource;

          /**
          * ResponseBefore constructor.
          *
          * @param MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          */
          public function __construct(
          MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          ) {
          $this->productUrlRewriteResource = $productUrlRewriteResource;
          }

          /**
          * @param Observer $observer
          * @return void
          */
          public function execute(Observer $observer)
          {
          /** @var MagentoFrameworkAppRequestInterface $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.






          share|improve this answer


























          • Thanks for the answer. +1

            – Shoaib Munir
            8 hours ago
















          1














          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 VendorModuleObserver;

          use MagentoFrameworkEventObserver;
          use MagentoFrameworkEventObserverInterface;

          /**
          * Class ResponseBefore
          *
          */
          class ResponseBefore implements ObserverInterface
          {
          /**
          * @var MagentoCatalogUrlRewriteModelResourceModelCategoryProduct
          */
          private $productUrlRewriteResource;

          /**
          * ResponseBefore constructor.
          *
          * @param MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          */
          public function __construct(
          MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          ) {
          $this->productUrlRewriteResource = $productUrlRewriteResource;
          }

          /**
          * @param Observer $observer
          * @return void
          */
          public function execute(Observer $observer)
          {
          /** @var MagentoFrameworkAppRequestInterface $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.






          share|improve this answer


























          • Thanks for the answer. +1

            – Shoaib Munir
            8 hours ago














          1












          1








          1







          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 VendorModuleObserver;

          use MagentoFrameworkEventObserver;
          use MagentoFrameworkEventObserverInterface;

          /**
          * Class ResponseBefore
          *
          */
          class ResponseBefore implements ObserverInterface
          {
          /**
          * @var MagentoCatalogUrlRewriteModelResourceModelCategoryProduct
          */
          private $productUrlRewriteResource;

          /**
          * ResponseBefore constructor.
          *
          * @param MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          */
          public function __construct(
          MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          ) {
          $this->productUrlRewriteResource = $productUrlRewriteResource;
          }

          /**
          * @param Observer $observer
          * @return void
          */
          public function execute(Observer $observer)
          {
          /** @var MagentoFrameworkAppRequestInterface $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.






          share|improve this answer















          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 VendorModuleObserver;

          use MagentoFrameworkEventObserver;
          use MagentoFrameworkEventObserverInterface;

          /**
          * Class ResponseBefore
          *
          */
          class ResponseBefore implements ObserverInterface
          {
          /**
          * @var MagentoCatalogUrlRewriteModelResourceModelCategoryProduct
          */
          private $productUrlRewriteResource;

          /**
          * ResponseBefore constructor.
          *
          * @param MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          */
          public function __construct(
          MagentoCatalogUrlRewriteModelResourceModelCategoryProduct $productUrlRewriteResource
          ) {
          $this->productUrlRewriteResource = $productUrlRewriteResource;
          }

          /**
          * @param Observer $observer
          * @return void
          */
          public function execute(Observer $observer)
          {
          /** @var MagentoFrameworkAppRequestInterface $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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 13 hours ago

























          answered 14 hours ago









          Siarhey UchukhlebauSiarhey Uchukhlebau

          9,97693058




          9,97693058













          • Thanks for the answer. +1

            – Shoaib Munir
            8 hours ago



















          • Thanks for the answer. +1

            – Shoaib Munir
            8 hours ago

















          Thanks for the answer. +1

          – Shoaib Munir
          8 hours ago





          Thanks for the answer. +1

          – Shoaib Munir
          8 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f268938%2fget-current-product-id-in-observer-controller-front-send-response-before%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What other Star Trek series did the main TNG cast show up in?

          Berlina muro

          Berlina aerponto