Magento – Magento 2 : How to add a product grid (assign product) in custom module using UI Component

gridmagento2moduleproduct

I want to add product grid (assign product) in my custom module using UI Component.

I follow this ANSWER:

Magento 2 : How to add a product grid in custom module using ui component?

and I get the error like:

Fatal error: Call to undefined method
Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::getProductsReadonly()
in
/home/public_html/magento2110/app/code/[Namespace]/[Module]/Block/Adminhtml/Products/Edit/Tab/Product.php
on line 133

If you already fixed above error so please help me to solve it or
If you have another way to fix it so please let me know.

Thanks in Advance!

Best Answer

Issue is you are calling a wrong method with product object in

Step3: Create Product.php in Namespace\Module\Block\Adminhtml\Products\Edit\Tab\

Just comment following code block in above example,

/*if ($this->getItem()->getProductsReadonly()) {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            $this->getCollection()->addFieldToFilter('entity_id', ['in' => $productIds]);
        } */
Related Topic