5

I have requirement like below.

I need to read all quote items and their sku, against the custom table,

If value matched, need to delete that quote item and update qty 1 for remaining items but this code is not working for me,

Class DeleteItem extends \Magento\Framework\App\Action\Action{{
 protected $checkoutSession;    
 protected $cart;
 protected $_resource;
 public function __construct(
  \Magento\Framework\App\Action\Context $context,
  \Magento\Checkout\Model\Session $checkoutSession,
  \Magento\Checkout\Model\Cart $cart,
 \Magento\Framework\App\ResourceConnection $resource,
 { 
   $this->cart = $cart;
   $this->checkoutSession = $checkoutSession;
  $this->_resource = $resource;
   parent::__construct($context);
 }

public function execute() {
   $quote = $this->checkoutSession->getQuote();
            $quoteItems = $quote->getAllVisibleItems();
            foreach($quoteItems as $item) {                    
                $productSku = $item->getProduct()->getSku();                    
                $tableSKu  = $this->checkIfExist($productSku);
                if($tableSKu){
                    $item->delete();
                    continue;
                }                       

                $item->setQty(1);
                $item->save();
            }
        $message = "You alredy bought this Product";
            $this->messageManager->addError(__($message));
            $cartUrl = $this->_url->getUrl('checkout');
            $this->_responseFactory->create()->setRedirect($cartUrl)->sendResponse();            
            exit;
       }
public function checkIfExist($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;
    }
    }
 public function getConnection(){
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    return $connection;
  }

 }

Right now the item is deleting but if navigated to cart page, the summary being shown right hand side, where item in cart is empty

Can anyone look into this and update me the solution,

Thanks

3
  • this doesn't solve your problem, but you should never write SQL queries Commented Jun 14, 2019 at 9:29
  • @PhilippSander, okay, right now item is deleting but cart summary is still there
    – Manjunath
    Commented Jun 14, 2019 at 9:31
  • @PhilippSander, Can you provide me any solution for it please
    – Manjunath
    Commented Jun 14, 2019 at 10:27

2 Answers 2

3

Try below code:

use Magento\Framework\Controller\ResultFactory;
Class DeleteItem extends \Magento\Framework\App\Action\Action{{
     protected $checkoutSession;    
     protected $cart;
     protected $_resource;
     protected $quoteItem;
       protected $resultRedirect;
       protected $messageManager;
     public function __construct(
      \Magento\Framework\App\Action\Context $context,
      \Magento\Checkout\Model\Session $checkoutSession,
      \Magento\Checkout\Model\Cart $cart,
     \Magento\Framework\App\ResourceConnection $resource,
      \Magento\Quote\Model\Quote\Item $quoteItem,
      \Magento\Framework\Message\ManagerInterface $messageManager,
      ResultFactory $resultRedirect
     { 
       $this->cart = $cart;
       $this->checkoutSession = $checkoutSession;
       $this->_resource = $resource;
       $this->quoteItem=$quoteItem;
       $this->resultRedirect=$resultRedirect;
       $this->messageManager = $messageManager;
       parent::__construct($context);
     }

    public function execute() {
       $quote = $this->checkoutSession->getQuote();
                $quoteItems = $quote->getAllVisibleItems();
                foreach($quoteItems as $item) {                    
                    $productSku = $item->getProduct()->getSku();                    
                    $tableSKu  = $this->checkIfExist($productSku);
                    $itemid=$item->getItemId();
                    if($tableSKu){
                         $quoteItem=$this->quoteItem->load($itemid);
                         $quoteItem->delete();//deletes the item

                        continue;
                    }                       

                    $item->setQty(1);
                    $item->save();
                }
               $this->messageManager->addError( __('Item deleted Successfully !') );
               $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
               $resultRedirect->setPath("checkout");
               return $resultRedirect;

           }
    public function checkIfExist($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;
        }
        }
     public function getConnection(){
        $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
        return $connection;
      }

     }

I have just modified your code to use quoteItem Model because it seems that your code is deleting item from collection but not from checkoutSession.

10
  • Yes, this one didn't work for me @Khushbu
    – Manjunath
    Commented Jun 14, 2019 at 10:44
  • hi @khushbu, I need to show error message with redirect to checkout, but its not showing for me. canu help me pls
    – Manjunath
    Commented Jul 12, 2019 at 11:04
  • When you want to show error message?
    – Khushbu
    Commented Jul 12, 2019 at 11:10
  • After deleting item..
    – Manjunath
    Commented Jul 12, 2019 at 11:15
  • I have updated code. Try that
    – Khushbu
    Commented Jul 12, 2019 at 11:20
1

If using quoteItem doesn't work then you should try to update cart data as below:

Create sections.xml file in

Vendor/Package/etc/frontend

Write below code in that

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="routeid/folder/DeleteItem"> <!-- Your action path -->
        <section name="cart"/>
    </action>
</config>

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.