1

I have retrieving all cart items and deleting the quote item by match with the product Sku,

Here is my code.

protected $checkoutSession;
protected $_itemModel;
protected $cart;
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Checkout\Model\Cart $cart,
    \Magento\Quote\Model\Quote\Item $itemModel
    { 
     $this->cart = $cart;
     $this->checkoutSession = $checkoutSession;
     $this->_itemModel = $itemModel;
     parent::__construct($context);
    }

 public function execute() {
       $quote = $this->checkoutSession->getQuote();
                $quoteItems = $quote->getAllVisibleItems();
                foreach($quoteItems as $item) {
                    $cartquote = $this->cart->getQuote();   

                    $productSku = $item->getProduct()->getSku();
                    //$this->logger->info('ItemproductSku'.$productSku);
                    $checkExistedSKu  = $this->getExistedSKu($productSku);
                    if($checkExistedSKu){
                        $itemId = $item->getItemId();
                        //$this->cart->removeItem($itemId)->save();
                        $this->_itemModel->load($item->getItemId())->delete();
                    }                       

                    $cartquote->updateItem($item->getId(), array( 'qty' => 1));
                    $cartquote->save();
                }

    public function getExistedSKu($productSku){
       $connection = $this->getConnection();
       $sql = "select * from custom_table where sku='".$productSku."'";
       $resultProduct = $connection->query($sql);
       $resultQuery = $resultProduct->fetchAll();

      if(!empty($resultQuery)){ 
        $parent_sku = $resultQuery[0]['parent_sku'];
        return true;
       }
      }else{
       return false;
      }
      }

I am getting the product Sku by itemId and checking against my custom table, if Product Sku present in the table I am deleting that quote item and updating all other Items by Qty 1.

This code is not working for me, can anyone look into it where i am doing wrong here,

Any help will be Appreciated!!

2

Update for yourr comment

foreach($quoteItems as $item) {

                $productSku = $item->getSku();
                $checkExistedSKu  = $this->getExistedSKu($productSku);
                if($checkExistedSKu){
                    $item->delete();
                    continue;
                }                       

                $item->setQty(1);
                $item->save();
            }
$quote->collectTotals();
5
  • hi, can you update to my answer please, I need to set all remaining items as Qty 1, so i used above code, If i need to use your code, where to include that line, pls update me full answer May 20 '19 at 11:37
  • updated comment! pls check
    – the light
    May 20 '19 at 11:43
  • $quote->save() is not required? at the end.. May 20 '19 at 11:45
  • is it not required? I am going to use your code now May 20 '19 at 11:53
  • this is not working, Item deleting from the cart, but not showing in list, instead only total showing in the summary Jun 14 '19 at 8:26

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.