0

How to get product in product edit page...

In model file

XXX\Stock\Model\ResourceModel\Edit\Grid\Collection

protected function _initSelect()
{    
    parent::_initSelect();
    $order = $this->registryManager->registry('current_product');
}

I can't get id in this file....

Thanks.

  • are you trying to get it in admin side product edit page ? – Dhaval Solanki Mar 22 '18 at 8:26
  • Yes............ – Mahi M Mar 22 '18 at 8:27
  • you can also get it from url check url there will be one param called "Id" you can get it $this->getRequest()->getParam("id"); so you will get product id – Dhaval Solanki Mar 22 '18 at 8:35
  • Its not working – Mahi M Mar 22 '18 at 8:41
  • can you post your code where you are not able to get ID ? – Dhaval Solanki Mar 22 '18 at 8:49
2

You should inject the request class to use it.

protected $request;

public function __construct(
    \Magento\Framework\App\Request\Http $request,
    ....//rest of parameters here
) {
   $this->request = $request;
   ...//rest of constructor here
}
protected function _initSelect()
{
    $productId = $this->request->getParam('id', false);
}

Note: Clear your generated folder if you have injected any new class. You will get id only when the product is saved. Your url will look something like this if its a product edit form

http://example.com/admin/catalog/product/edit/id/374/key/fe1f40f9bb9379438423a4e345134df7c64a0c122dc54cbac933f8d5a6b23548/
  • still i'm not getting – Mahi M Mar 22 '18 at 9:00
  • Above code is perfect and it should be working . can you please share what error you are facing in above code ? – Dhaval Solanki Mar 22 '18 at 9:19
  • I have add custom tab in product edit page and custom grid for that tab using modifier.Error:Open popup ,it shows "something went wrong" – Mahi M Mar 22 '18 at 9:24
  • I want filter the table collection using product id – Mahi M Mar 22 '18 at 9:25

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.