我需要从购物车中删除 简单产品。
删除项目选项仅适用于可配置产品
<a href="#"
title="<?php echo $block->escapeHtml(__('Remove item')); ?>"
class="action action-delete btn-remove btn-remove2 remove-btn"
data-post='<?php /* @escapeNotVerified */ echo $block->getDeletePostJson(); ?>'>
<i class="fa fa-times-circle" aria-hidden="true"></i>
<?php /* <span>
<?php /* @escapeNotVerified * / echo __('Remove item')?>
</span> */ ?>
</a>
以上代码适用于可配置产品
答案 0 :(得分:0)
要从购物车中删除商品,您需要遵循以下功能:
public function deleteQuoteItems(){
$checkoutSession = $this->getCheckoutSession();
$allItems = $checkoutSession->getQuote()->getAllVisibleItems();//returns all teh items in session
foreach ($allItems as $item) {
$itemId = $item->getItemId();//item id of particular item
$quoteItem=$this->getItemModel()->load($itemId);//load particular item which you want to delete by his item id
$quoteItem->delete();//deletes the item
}
}
public function getCheckoutSession(){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager
$checkoutSession = $objectManager->get('Magento\Checkout\Model\Session');//checkout session
return $checkoutSession;
}
public function getItemModel(){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager
$itemModel = $objectManager->create('Magento\Quote\Model\Quote\Item');//Quote item model to load quote item
return $itemModel;
}