2

How do I get the current productId on the product edit page.

$request->getParam('current_product_id');

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();  
    $request = $objectManager->get('Magento\Framework\App\Request\Http');  
    $product_id = $request->getParam('current_product_id');
   
1
  • Have you tried with registry, <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product echo $product->getId();
    – Mohit Rane
    Nov 15, 2021 at 8:16

3 Answers 3

2

1: Create one custom block file on your extension let said : Blockname.php

app\code\Vendor\Extension\Block\Blockname.php

<?php
namespace Vendor\Extension\Block;
 
use Magento\Framework\View\Element\Template;
 
class Blockname extends Template
{
 
    protected $_registry;
 
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
    )
    {
        $this->_registry = $registry;
        parent::__construct($context);
    }
 
    public function getCurrentProduct()
    {
        return $this->_registry->registry('current_product');
    }
 
}
  1. Now, we will get the current product in your custom template phtml file like this one :

    if ($currentProduct = $block->getCurrentProduct()) {
     echo $currentProduct->getId() . '<br />';
     echo $currentProduct->getName() . '<br />';
     echo $currentProduct->getSku() . '<br />';
     echo $currentProduct->getFinalPrice() . '<br />';
     echo $currentProduct->getProductUrl() . '<br />'; 
    }
    

NOTE: Remember, you custom block file must be set as block type of your phtml file.

0

You should not use the $objectManager, but instead use repositories. If you still want to use the $objectManager, then do $request->getParam('product_id');

0

If you are in edit product page (BackEnd) i think param should be id. And should like this $request->getParam('id');

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.