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;

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

                    $productSku = $item->getProduct()->getSku();
                    $checkExistedSKu  = $this->getExistedSKu($productSku);
                        $itemId = $item->getItemId();

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

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

        $parent_sku = $resultQuery[0]['parent_sku'];
        return true;
       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!!

1 Answer 1


Update for yourr comment

foreach($quoteItems as $item) {

                $productSku = $item->getSku();
                $checkExistedSKu  = $this->getExistedSKu($productSku);

  • 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 Commented May 20, 2019 at 11:37
  • updated comment! pls check
    – the light
    Commented May 20, 2019 at 11:43
  • $quote->save() is not required? at the end.. Commented May 20, 2019 at 11:45
  • is it not required? I am going to use your code now Commented May 20, 2019 at 11:53
  • this is not working, Item deleting from the cart, but not showing in list, instead only total showing in the summary Commented Jun 14, 2019 at 8:26

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.