0

In my requirement, I need product id from the admin product edit page on how to achieve this in Magento 2. I have tried $productId = $this->request->getParam('id'); but it is empty

Model File:-

<?php

namespace Vendor\Module\Model\ResourceModel\Collection;

use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    /**
     * @var string
     */
    protected $_idFieldName = 'sku';

    protected $_isPkAutoIncrement = false;


    /**
     * Define resource model.
     */
    const YOUR_TABLE = 'custom_table';

    protected $helper; public function __construct(
        \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
        \Vendor\Module\Helper\Data $helper
    ) {

        $this->storeManager = $storeManager;
        $this->helper = $helper;

        parent::__construct(
            $entityFactory,
            $logger,
            $fetchStrategy,
            $eventManager,
            $connection,
            $resource
        );
    }



    protected function _construct()
    {
        $this->_init('Vendor\Module\Model\Post', 'Vendor\Module\Model\ResourceModel\Post');
    }

    protected function _initSelect()
    {
        parent::_initSelect();

        $area = $this->helper->getArea();

        if ($area == 'adminhtml') {

             $redirectUrl = $this->helper->getCurrentProduct();
             $this->getRequest()->getParam('id');
             $attributeUrl = explode("/", $redirectUrl);
             $productId =  '23';
             $product = $this->helper->getLoadProduct($productId);
             $sku = $product->getSku();
             $customtag = $product->getCustomtags();
             $customtagsarr = explode("|",$customtag);
             $customvalues = "'" . implode ( "', '", $customtagsarr ) . "'";
            $this->getSelect()->orwhere("main_table.customtags IN($customvalues)");
        } else {
            $this->getSelect();
        }
    }
}

2 Answers 2

0

This code is create simple custom module and Try some code :-

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    echo $product->getId();
    echo $product->getName();
?>

OR

Add bellow code in your block file.

for example app/code/yourvendor/yourmodulename/Block/CustomBlock.php

<?php
namespace AR\CustomModule\Block;
class CustomBlock extends \Magento\Framework\View\Element\Template
{
    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,       
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {       
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getCurrentCategory()
    {       
        return $this->_registry->registry('current_category');
    }

    public function getCurrentProduct()
    {       
        return $this->_registry->registry('current_product');
    }   

}
?>

Printing current product data in your template (custom.phtml) file

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

Hope this help

Thanks ...

2
  • I have tried but returns empty
    – kanidhaya
    Commented May 28, 2020 at 12:20
  • Any idea to get product id from admin panel
    – kanidhaya
    Commented May 28, 2020 at 15:11
0

check this

add the below code into your template file.

echo $this->getRequest()->getParam('id');

Add bellow code in your Block file.

<?php

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Registry;

class StockLeft extends Template
{
    private $_registry;

    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_registry = $registry;
    }

    protected function getCurrentProductID() 
    {
        return $this->_registry->registry('current_product')->getId();
    }

}

Output:-

enter image description here

17
  • Hi i have tried but it also gives the empty value
    – kanidhaya
    Commented May 28, 2020 at 16:59
  • $productId = $this->request->getParam('id'); instead of $this->getRequest()->getParam('id');
    – Devidas
    Commented May 28, 2020 at 17:00
  • I need the product id in product edit page from backend i have tried the above ways but i did't get the value ....please give me any idea about this ,it will be helpful
    – kanidhaya
    Commented May 28, 2020 at 17:01
  • i have tried the above way also $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $request = $objectManager->get('Magento\Framework\App\Request\Http'); echo $param = $request->getParam('id');
    – kanidhaya
    Commented May 28, 2020 at 17:02
  • you just add my code into the vendor/magento/module-theme/view/adminhtml/templates/title.phtml and flush cache and check if it works or not for checking
    – Devidas
    Commented May 28, 2020 at 17:04

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.