如何删除简单的产品形式推车在magento 2.1



我需要从购物车中删除简单产品

删除仅适用于可配置产品的项目选项

<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>

以上代码适用于可配置产品

  1. 如何从购物车中删除简单产品?

要从购物车中删除商品,您需要遵循以下函数:

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 = MagentoFrameworkAppObjectManager::getInstance();//instance of object manager 
    $checkoutSession = $objectManager->get('MagentoCheckoutModelSession');//checkout session
    return $checkoutSession;
    }
    public function getItemModel(){
    $objectManager = MagentoFrameworkAppObjectManager::getInstance();//instance of object manager
    $itemModel = $objectManager->create('MagentoQuoteModelQuoteItem');//Quote item model to load quote item
    return $itemModel;
    }

最新更新