【问题标题】:Ajax delete product from cart in magentoAjax从magento中的购物车中删除产品
【发布时间】:2014-02-06 08:04:55
【问题描述】:

我一直在使用 magento cart pro 扩展。对于 ajax 添加到购物车的功能,我关注了以下链接 Excellance Magento Blog

它工作正常。但我已经搜索了很多关于从购物车中删除项目的 ajax,但谷歌总是用 3rd 方扩展返回我。我找不到从购物车中删除产品的教程。所以我想,从具有添加到购物车和从购物车删除功能的免费 3rd 方扩展中获取参考。我发现以下扩展具有上述功能和工作原理。 Ajax cart pro

我已经检查了这个扩展编码部分。但我真的很困惑,我找不到执行删除功能的代码。我认为他们只覆盖控制器文件中的 deleteAction。我什至无法理解我在做什么。我只需要添加 ajax 删除功能的指导。

如果有人对此有任何想法或找到任何教程,请与我分享您的想法朋友。

【问题讨论】:

    标签: php jquery ajax magento


    【解决方案1】:

    深入了解 Ajaxcart 扩展,在控制器的操作中,我们可以发现(是的,您是对的!),以下行实际上是删除一个项目:

    $this->_getCart()->removeItem($id)->save();

    public function deleteAction() {
        $id = (int) $this->getRequest()->getParam('id');
        if ($id) {
            try {
                $this->_getCart()->removeItem($id)
                        ->save();
            } catch (Exception $e) {
                $_response = Mage::getModel('ajaxcart/response');
                $_response->setError(true);
                $_response->setMessage($this->__('Cannot remove the item.'));
                $_response->send();
    
                Mage::logException($e);
            }
        }
    
        $_response = Mage::getModel('ajaxcart/response');
    
        $_response->setMessage($this->__('Item was removed.'));
    
        //append updated blocks
        $this->getLayout()->getUpdate()->addHandle('ajaxcart');
        $this->loadLayout();
    
        $_response->addUpdatedBlocks($_response);
    
        $_response->send();
    }
    

    所以回答你的问题“哪个代码在删除”,它肯定在里面;)

    要了解整个过程,您应该牢记以下几点:

    • ajaxcart.js - 他们覆盖 Magento 的 setLocation 函数,然后进行 ajax 调用(在 ajaxCartSubmit 方法中):

      var oldSetLocation = setLocation;
      var setLocation = (function() {
          return function(url){
             if( url.search('checkout/cart/add') != -1 ) {
                  //its simple/group/downloadable product
                  ajaxcart.ajaxCartSubmit(url);
      
    • 渲染按钮使用url helper,像这样:

      <button type="button" title="Add to Cart" class="button btn-cart"
          onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product, $additional = array('qty' => 1));?>')">
       +
      </button>
      
    • updateBlocks. 有一个 Observer 和 Response 模型可以获取要更新/替换的块列表在前端,渲染它们的内容并将它们放入 json 响应中。

      $updated_blocks = unserialize(Mage::getStoreConfig('ajaxcart/general/update_blocks'));
      // ... for each do a $layout->getBlock('<blockName>')->toHtml() and add to json responce
      
    • 从核心布局和附加 ajaxcart 句柄 (layout/ajaxcart.xml) 中获取的块定义。 (cart_sidebar、top.links 等)

    【讨论】:

    • 您已经写了将产品添加到购物车
    • 是的,它类似于“添加”。还有一个用于删除的辅助方法(或者您可以自己编写): $this->helper('checkout/cart')->getRemoveUrl($item)
    • 所有魔法都发生在 setLocation() 中,因此您需要将删除 url 传递给它。祝你好运!;)
    【解决方案2】:

    可以参考以下代码

    <?php
    $id = '100'; // replace product id with your id
    
    $cartHelper = Mage::helper('checkout/cart');
    $items = $cartHelper->getCart()->getItems();
    foreach($items as $item):
    if($item->getProduct()->getId() == $id):
    $itemId = $item->getItemId();
    $cartHelper->getCart()->removeItem($itemId)->save();
    break;
    endif;
    endforeach;
    ?>
    

    【讨论】:

    • 感谢您的回复。是的!我从核心文件中获得了删除操作代码。但我不知道,如何通过 ajax 调用这个控制器函数!
    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多