Get current product id in Observer controller_front_send_response_beforeMagento is caching 'catalog_controller_product_view' eventUsing observer, how to get the current pager with loaded product collectionPassing checkout shipping information to observerIs there any specific order of invoking observer functions?Order confirmation observer in Magento 2 not workingGet customer from save event observer Magento 2Get CatalogInventory stock collection in ObserverHow to get updated cart subtotal in checkout_cart_product_add_after event observer in Magento 2?Magento2: How to get shipping method in order using observer `sales_order_save_after`?Using observer event to get updated customer infoGet current product in observer Magento 2

Minkowski space

Can I make popcorn with any corn?

Today is the Center

Test whether all array elements are factors of a number

A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?

Is a tag line useful on a cover?

How to format long polynomial?

Languages that we cannot (dis)prove to be Context-Free

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

What would happen to a modern skyscraper if it rains micro blackholes?

How to find program name(s) of an installed package?

"to be prejudice towards/against someone" vs "to be prejudiced against/towards someone"

Finding angle with pure Geometry.

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Maximum likelihood parameters deviate from posterior distributions

Collect Fourier series terms

What typically incentivizes a professor to change jobs to a lower ranking university?

Show that if two triangles built on parallel lines, with equal bases have the same perimeter only if they are congruent.

Problem of parity - Can we draw a closed path made up of 20 line segments...

How does one intimidate enemies without having the capacity for violence?

Watching something be written to a file live with tail

Is it possible to do 50 km distance without any previous training?

How do I create uniquely male characters?

Mathematical cryptic clues



Get current product id in Observer controller_front_send_response_before


Magento is caching 'catalog_controller_product_view' eventUsing observer, how to get the current pager with loaded product collectionPassing checkout shipping information to observerIs there any specific order of invoking observer functions?Order confirmation observer in Magento 2 not workingGet customer from save event observer Magento 2Get CatalogInventory stock collection in ObserverHow to get updated cart subtotal in checkout_cart_product_add_after event observer in Magento 2?Magento2: How to get shipping method in order using observer `sales_order_save_after`?Using observer event to get updated customer infoGet current product in observer Magento 2






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







      Shoaib Munir

















      asked yesterday









      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
            yesterday











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

            – Shoaib Munir
            yesterday


















          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
            yesterday











          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
            yesterday











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

            – Shoaib Munir
            yesterday















          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
            yesterday











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

            – Shoaib Munir
            yesterday













          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 yesterday









          sv3n

          9,93662456




          9,93662456










          answered yesterday









          mohithmohith

          713




          713












          • let me know if any issues

            – mohith
            yesterday











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

            – Shoaib Munir
            yesterday

















          • let me know if any issues

            – mohith
            yesterday











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

            – Shoaib Munir
            yesterday
















          let me know if any issues

          – mohith
          yesterday





          let me know if any issues

          – mohith
          yesterday













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

          – Shoaib Munir
          yesterday





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

          – Shoaib Munir
          yesterday













          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
            yesterday















          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
            yesterday













          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 yesterday

























          answered yesterday









          Siarhey UchukhlebauSiarhey Uchukhlebau

          9,97693058




          9,97693058












          • Thanks for the answer. +1

            – Shoaib Munir
            yesterday

















          • Thanks for the answer. +1

            – Shoaib Munir
            yesterday
















          Thanks for the answer. +1

          – Shoaib Munir
          yesterday





          Thanks for the answer. +1

          – Shoaib Munir
          yesterday

















          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

          Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

          Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

          Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림