1

Magento 2 how to show catalog/product/list.phtml in Custom Module popup with custom filter product collection Anyone know how to do this ?

0

First, you need to create Vendor\Module\Block\ProductList.php and overwrite _getProductCollection() class in Magento\Catalog\Block\Product\ListProduct

protected function _getProductCollection()

  /*your code here*/  

    $this->_productCollection = $yourcollection;
    return $this->_productCollection;

Now, the collection has been moved to your phtml file

$block = $viewBlock->getLayout()
                ->createBlock('Vendor\Module\Block\ProductList')
                ->setTemplate('Magento_Catalog::product/list.phtml')
                ->toHtml();"
6
  • can we get filter collection using this ? – sumeet bajaj Mar 5 at 12:31
  • No. By doing the above, you will receive all products in the store. To be able to receive products by a filter you can see how in our example below. For initial use you need to declare the following classes in your __construct: \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Model\Product\Visibility $productVisibility, \Magento\Catalog\Helper\Category $categoryHelper – LitExtension Magento Migration Mar 6 at 5:13
  • public function _getProductCollection() { / get all category ids of current store / $websiteId = $this->storeManager->getStore()->getWebsiteId(); $currentStoreAllCategories = $this->categoryHelper->getStoreCategories(false,true,true); – LitExtension Magento Migration Mar 6 at 5:14
  • $collection = $this->_productCollectionFactory->create(); $collection->addAttributeToSelect(array('entity_id','sku')); // filter current website products $collection->addWebsiteFilter($websiteId); // set visibility filter $collection->setVisibility($this->productVisibility->getVisibleInSiteIds()); /*SKU Filter Here*/ $sku = array('24-MB04', '24-MB03', '24-MB02'); $collection->addAttributeToFilter('sku', array('in' => $sku)); – LitExtension Magento Migration Mar 6 at 5:14
  • //You can return items or data or whatever you want in this collection return $collection->getItem(); } – LitExtension Magento Migration Mar 6 at 5:14

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.