1

In Magento2, if we want the current category detail then we can retrieve it from Registry as below:

$currentCategory = $this->_registry->registry('current_category');

For Getting current product detail, then we can retrieve it from Registry as below:

$currentProduct = $this->_registry->registry('product');

OR

$currentProduct = $this->_registry->registry('current_product');

So which one we should use or which one that Magento recommends for getting Current Product? why?

Thanks...

0

Take a look:

\Magento\Catalog\Helper\Product::initProduct()

    // Register current data and dispatch final events
    $this->_coreRegistry->register('current_product', $product);
    $this->_coreRegistry->register('product', $product);

As we can see, Magento registers two variables. That why we can get by current_product and product variables. Seem that Magento wants to give us "more ways" to get the current product data in block, event,... via Registry.

See more:

\Magento\Catalog\Controller\Adminhtml\Category\Widget\CategoriesJson::execute()

    $this->_coreRegistry->register('category', $category);
    $this->_coreRegistry->register('current_category', $category);
  • Hello @Khoa, Thanks alot for your quick response. I am agreed in the case of getting current Product. But for getting current category specially on Frontend, we should only use: $currentCategory = $this->_registry->registry('current_category'); – Nilesh Tighare Jul 3 '17 at 9:14
  • Yes, in category case I showed, it belongs to Admin area. – Khoa TruongDinh Jul 3 '17 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.