문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top