doulai2025
2013-12-18 11:57
浏览 38
已采纳

Magento - 可在产品视图页面中从购物车中删除产品的选项

How do I add an option to remove a product from cart within product view page?

图片转代码服务由CSDN问答提供 功能建议

如何在产品视图页面中添加从购物车中删除产品的选项? < / DIV>

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtfpznrbn503027700 2013-12-18 12:52
    已采纳

    To remove an item from your cart by sku, you could create a controller with the below code

    $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;
        }
    }
    

    Then on your product view page you could create a ajax/button that post the sku you want to remove

    See Magento - Remove Item from Cart by Sku

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

    Create: 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();
        }
      }
    

    ?>

    Create: app/etc/modules/MagePal_DeleteCartItemBySku.xml

      <?xml version="1.0"?>
        <config>
               <modules>
                      <MagePal_DeleteCartItemBySku>
                              <active>true</active>
                              <codePool>local</codePool>
                      </MagePal_DeleteCartItemBySku>
               </modules>
        </config>
    

    Then your ajax/button url :

    domain.com/checkout/cart/removeItemBySku/sku/xyz

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题