2

I try to fetch quote item collection and add some filter like this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
$newItem = $objectManager->create('Magento\Quote\Model\ResourceModel\Quote\Item\Collection')
          ->addFieldToFilter('quote_id',$data['quote_id'])
          ->addFieldToFilter('store_id',$data['store_id'])
          ->addFieldToFilter('qty',$data['qty'])
          ->addFieldToFilter('sku',$data['sku'])->getFirstItem();
echo "<pre>";print_r($newItem->getData());exit;

but i got an error like this:

PHP Fatal error: Uncaught Error: Call to a member function getStoreId() on null in /var/www/magento/vendor/magento/module-quote/Model/ResourceModel/Quote/Item/Collection.php:98

2 Answers 2

4

Currently the quote item collection must be initialized with an actual quote model to work:

$quoteItemCollection->setQuote($quote);

This already has been reported as a bug: https://github.com/magento/magento2/issues/8954

Luckily, to load the collection, it's only needed to retrieve the store id, so you don't need to actually load the quote:

$quoteItemCollection = $objectManager->create(\Magento\Quote\Model\ResourceModel\Quote\Item\Collection::class);
$quote = $objectManager->create(\Magento\Quote\Model\Quote::class)

$quote->setStoreId($data['store_id']);

$quoteItemCollection
    ->setQuote($quote)
    ->addFieldToFilter('quote_id',$data['quote_id'])
    ->addFieldToFilter('qty',$data['qty'])
    ->addFieldToFilter('sku',$data['sku']);
$newItem = $quoteItemCollection->getFirstItem();

Note: Do not use the object manager directly. In real code, $quoteItemCollection and $quote would be created by factories, which are injected into the class where this code is.

0
0

If you want a particular quote's item filter then , then you can try below and here i have loaded quote object then filter the quote item collection by this quote.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$CollectionFactory = $objectManager->get('\Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory');
$newItems=$CollectionFactory->create();

/* load quote **/
$quoteFactory = $objectManager->create('\Magento\Quote\Model\QuoteFactory');
$quote = $quoteFactory->create()->load($data['quote_id']);


/* filter Item Collection  by quote */
$newItems
    ->setQuote($quote)
    ->addFieldToFilter('store_id',$data['store_id'])
    ->addFieldToFilter('qty',$data['qty'])
    ->addFieldToFilter('sku',$data['sku']);
$newItem = $newItems->getFirstItem();
echo "<pre>";print_r($newItem->getData('sku'));exit;

Multiple want to filter with multiple quote ids then you should try this:

$resource = $objectManager ->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('quote_item');

$query =  $connection->select()->reset()
    ->from(['main_table' => $tableName], '*')
   // ->where('main_table.quote_id = ?',$data['quote_id'])
    ->where('main_table.quote_id IN (?)',array($quoteId1,$quoteId1))
    ->where('main_table.store_id= ?',$data['store_id'])
    ->where('main_table.qty = ?',$data['qty'])
    ->where('main_table.sku= ?',$data['sku'])
    ->limit(0,1) // =  getFirstItem();
    ;

$result = $connection->fetchAll($query);

echo "<pre>";print_r($result);exit;

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.