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;
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
add a comment |
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
add a comment |
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
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
magento2 event-observer
edited yesterday
Shoaib Munir
asked yesterday
Shoaib MunirShoaib Munir
2,3692829
2,3692829
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
let me know if any issues
– mohith
yesterday
I tried this. It is that simple and it's working. Thanks
– Shoaib Munir
yesterday
add a comment |
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.
Thanks for the answer. +1
– Shoaib Munir
yesterday
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
let me know if any issues
– mohith
yesterday
I tried this. It is that simple and it's working. Thanks
– Shoaib Munir
yesterday
add a comment |
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
let me know if any issues
– mohith
yesterday
I tried this. It is that simple and it's working. Thanks
– Shoaib Munir
yesterday
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Thanks for the answer. +1
– Shoaib Munir
yesterday
add a comment |
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.
Thanks for the answer. +1
– Shoaib Munir
yesterday
add a comment |
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.
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.
edited yesterday
answered yesterday
Siarhey UchukhlebauSiarhey Uchukhlebau
9,97693058
9,97693058
Thanks for the answer. +1
– Shoaib Munir
yesterday
add a comment |
Thanks for the answer. +1
– Shoaib Munir
yesterday
Thanks for the answer. +1
– Shoaib Munir
yesterday
Thanks for the answer. +1
– Shoaib Munir
yesterday
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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