0

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();

1 Answer 1

1

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();
12
  • $this->setCollection($filter_product); is not usefull ?
    – HaFiz Umer
    Commented May 20, 2019 at 11:17
  • It can also be used. But for that also, use need to create that variable as collection factory and push product array using addItem before set collection
    – aravind
    Commented May 20, 2019 at 11:25
  • Have you tried above method?did you get any error?
    – aravind
    Commented May 20, 2019 at 11:26
  • Sir i am confuse where you setCollection() ?
    – HaFiz Umer
    Commented May 20, 2019 at 11:27
  • If you want to create your own custom collection then setCollection can be used..
    – aravind
    Commented May 20, 2019 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.