如何在产品视图页面中添加从购物车中删除产品的选项?
答案 0 :(得分:1)
要通过sku从购物车中删除商品,您可以使用以下代码
创建控制器$sku = 'xyz';
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getSku() == $sku) {
$cartHelper->getCart()->removeItem($item->getItemId())->save();
break;
}
}
然后在您的产品视图页面上,您可以创建一个发布要删除的sku的ajax /按钮
请参阅Magento - Remove Item from Cart by Sku
创建:app / code / local / MagePal / DeleteCartItemBySku / etc / config.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_DeleteCartItemBySku>
<version>0.0.1</version>
</MagePal_DeleteCartItemBySku>
</modules>
<frontend>
<routers>
<checkout>
<use>standard</use>
<args>
<modules>
<MagePal_DeleteCartItemBySku after="Mage_Checkout">MagePal_DeleteCartItemBySku</MagePal_DeleteCartItemBySku>
</modules>
</args>
</checkout>
</routers>
</frontend>
<global>
<models>
<deletecartitembysku>
<class>MagePal_DeleteCartItemBySku_Model</class>
</deletecartitembysku>
</models>
</global>
</config>
创建:app / code / local / MagePal / DeleteCartItemBySku / controllers / CartController.php
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'CartController.php';
//require_once 'Mage/Checkout/controllers/CartController.php';
class MagePal_DeleteCartItemBySku_CartController extends Mage_Checkout_CartController
{
public function removeItemBySkuAction()
{
$sku = $this->getRequest()->getParam('sku');
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
$found = false;
foreach ($items as $item) {
if ($item->getProduct()->getSku() == $sku) {
$cartHelper->getCart()->removeItem($item->getItemId())->save();
$found = true;
break;
}
}
$message = ($found) ? "Item {$sku} Removed" : "Item {$sku} Not Found";
Mage::getSingleton('core/session')->addSuccess($message);
//Mage::getSingleton('core/session')->addError($message);
$this->_redirectReferer();
}
}
&GT?;
创建:app / etc / modules / MagePal_DeleteCartItemBySku.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_DeleteCartItemBySku>
<active>true</active>
<codePool>local</codePool>
</MagePal_DeleteCartItemBySku>
</modules>
</config>
然后你的ajax / button url:
domain.com/checkout/cart/removeItemBySku/sku/xyz