duanliexi1052 2016-04-18 05:29
浏览 18
已采纳

Magento Ajax购物车在删除重定向到删除页面

When I want to delete more than 1 product one by one from AJAX Cart , for 1st product its working fine but when I try to delete the another product the page it redirect to delete page URL like (SiteURL/checkout/cart/delete/id/012/uenc/aHR0cDovLzEyMi)

and showing some json or html data like {"message":"Item was removed.","update_blocks":[{"key":".header .links","value":"<div class=\"links\"> ....

Delete Item From Shopping cart function deleteAction()

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

I will highly appreciate if i can get some help. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dse84825 2016-04-20 08:48
    关注

    I got a solution, its related to a Javascript binding problem. The problem is, when you delete a product from the cart all the cart block gets updated. Hence it removes the already bound javascript.

    I have added the javascript bind code at the end of updateBlocks function in ajaxcart.js.

    So, whenever user tries to delete a item from the cart ajax code gets executed and the response update_blocks will once again gets bind to the cart even after the initial javascript binding.

    Added Javascript bind code is

    $$('a[href*="/checkout/cart/delete/"]').each(function (e) {    
                $(e).observe('click', function (event) {    
                    setLocation($(e).readAttribute('href'));    
                    Event.stop(event);    
                });    
            });
    

    And after adding code to ajaxcart.js updateBlocks function look like ,

    updateBlocks: function (blocks) {    
            var _this = this;
            if (blocks) {    
                try {    
                    blocks.each(function (block) {    
                        if (block.key) {    
                            var dom_selector = block.key;    
                            if ($$(dom_selector)) {    
                                jQuery(block.key, parent.document).each(function (e) {    
                                    jQuery(this).html(block.value);    
                                });    
                            }    
                        }    
                    });    
                    _this.bindEvents();    
                    _this.bindNewEvents();    
                    // show details tooltip    
                    truncateOptions();    
                } catch (e) {    
                    console.log(e);    
                }    
            }    
            $$('a[href*="/checkout/cart/delete/"]').each(function (e) {    
                $(e).observe('click', function (event) {    
                    setLocation($(e).readAttribute('href'));    
                    Event.stop(event);    
                });    
            });    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动