Question

I have 2 collection 1 is $bestsalecollection that show in dashboard . second is product collection. After filter collection when i return it generate error Call to a member function isLoaded() on array

Please any one help me to tell how filter array convert into magento collection ?

Thank you

 $bestsalecollection = $this->_collectionFactory->create()->setModel(
                \Magento\Catalog\Model\Product::class
            )->addStoreFilter(
                $storeId
            );
            $productCollection = $this->productcollectionFactory->create();
            $sellercollection=$productCollection->addFieldToFilter('current_user_id',array('eq'=>1));
            $filter_product=[];

            foreach ($sellercollection as $allProduct){
                foreach ($bestsalecollection as $bestSale){
                    if($allProduct->getEntityId()==$bestSale->getProductId()){
                        $filter_product[]=$bestSale;
                    }
                }
            }
    $this->setCollection($filter_product);
        return parent::_prepareCollection();
Was it helpful?

Solution

Inject Magento\Framework\Data\CollectionFactory in constructor for $this->collectionFactory

$bestsalecollection = $this->_collectionFactory->create()->setModel(
            \Magento\Catalog\Model\Product::class
        )->addStoreFilter(
            $storeId
        );

        $productCollection = $this->productcollectionFactory->create();
        $sellercollection=$productCollection->addFieldToFilter('current_user_id',array('eq'=>1));
        $filter_product=$this->collectionFactory->create();

        foreach ($sellercollection as $allProduct){
            foreach ($bestsalecollection as $bestSale){
                if($allProduct->getEntityId()==$bestSale->getProductId()){
                    $varienObject = new \Magento\Framework\DataObject();
                    $varienObject->setData($bestSale->getData());
                    $filter_product->addItem($varienObject);
                }
            }
        }
    $this->setCollection($filter_product);
    return parent::_prepareCollection();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top