2

I am creating a custom grid on the product edit page, for the grid collection I need to filter based on the product id, for that I am trying to get the product id from the URL using below code

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

But the above code is always throwing empty result. Also, I tried to get the value from the registry using the below code

protected $_registry;
public function __construct(\Magento\Framework\Registry $registry) { 
    $this->_registry = $registry;
}

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

But from the registry itself, I am unable to get the current product id.

I need the product id from URL or else from the registry, how to get that. Thanks in advance for your support and idea

full code

namespace Namespace\Module\Ui\DataProvider\Product;
use Magento\Framework\App\RequestInterface;
use Magento\Ui\DataProvider\AbstractDataProvider;

class QuestionAnswerDataProvider extends AbstractDataProvider { 

    protected $request; 
    public function __construct( 

        RequestInterface $request,

    ) { 
        $this->request = $request;
    }

    public function getData() {
       echo $this->request->getParam('id',false);
    }
}
0

Try this one

public function __construct(
    \Magento\Framework\App\Request\Http $request
) {
   $this->request = $request;
}

and then use

$productId = $this->request->getParam('id', false);
  • returning empty – senthil Feb 19 at 4:43
0

You can use the below code to get product Id.

<?php
namespace Training\Crud\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\App\RequestInterface;

class Create extends Template
{
    /**
     *
     * @var RequestInterface
     */
    protected $request;

    /**
     * Constructor
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(
        Template\Context $context, 
        RequestInterface $request,
        array $data = array()
    ) {
        parent::__construct($context, $data);
        $this->request = $request;
    }

    /**
     * Get Product Id
     */
    public function getProductId()
    {
        $productId = $this->request->getParam('id');

       return $productId;
    }
}
  • I have tried as per the above code, but still i am getting empty – senthil Feb 18 at 13:46

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.