0

Actually i want to filter the checkout cart collection using custom attributes values using this link

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$quoteId = $objectManager-> create(‘Magento\Checkout\Model\Session’)->getQuoteId();
$cartData = $objectManager->create(‘Magento\Quote\Model\QuoteRepository’)->get($quoteId)->getAllVisibleItems(); 
$currentItemCount = count($cartData); //Current cart quote count echo $currentItemCount;
2
  • what you want here please clarify Commented Feb 1, 2019 at 8:53
  • @Rakesh,Actually this code is for checkout cart collection and I want to filter this collection using custom attributes Commented Feb 1, 2019 at 9:24

1 Answer 1

0
<?php 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$item = $objectManager->create('Magento\Catalog\Model\Product');
$quoteId = $objectManager-> create('Magento\Checkout\Model\Session')->getQuoteId();
$cartData = $objectManager->create('Magento\Quote\Model\QuoteRepository')->get($quoteId)->getAllVisibleItems(); 

foreach($items as $item) {
    $item = $item->load($item->getProductId());

    if($item->getData('custom_attribute')=='SM100'){
        echo 'ID: '.$item->getProductId().'<br />';
        echo 'Name: '.$item->getName().'<br />';
        echo 'Sku: '.$item->getSku().'<br />';
        echo 'Quantity: '.$item->getQty().'<br />';
        echo 'Price: '.$item->getPrice().'<br />';
        echo "<br />";   
    }


  }
6
  • ,i have same code but need to add filter this collection with custom attributes as like we filter addAttributeToFilter('product_type','apple') Commented Feb 1, 2019 at 11:26
  • put condition in for loop if($item->getSku()=='SM100'){} it is only way to do this Commented Feb 1, 2019 at 11:28
  • ,getSku is the default attribute Commented Feb 1, 2019 at 11:30
  • you can add your custom, first load product in for loop i have edit my answer Commented Feb 1, 2019 at 11:34
  • ,Magento\Catalog\Model\Product wont give current added products to the cart. Commented Feb 4, 2019 at 6:27

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.