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;
  • what you want here please clarify – Rakesh Donga Feb 1 at 8:53
  • @Rakesh,Actually this code is for checkout cart collection and I want to filter this collection using custom attributes – hitesh balpande Feb 1 at 9:24
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 />";   
    }


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

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.