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.

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

当我想从AJAX Cart中逐个删除多个产品时,对于第一个产品,它的工作正常,但是当 我尝试删除另一个产品,它重定向的页面删除页面URL,如 (SiteURL / checkout / cart / delete / id / 012 / uenc / aHR0cDovLzEyMi)

并显示一些json或html数据,如 {“message”:“Item was removed。”,“update_blocks”:[{“key”:“。header .links”,“value”:“&lt; div class = \“links \”&gt; ....

从购物车中删除项目函数deleteAction()

 <  code> public function deleteAction(){
 $ id =(int)$ this-&gt; getRequest() - &gt; getParam('id'); 
 if($ id){
 try {
 $ this  - &gt; _getCart() - &gt; removeItem($ id)
  - &gt; save(); 
} catch(Exception $ e){
 $ _response = Mage :: getModel('ajaxcart / response');  
 $ _response-&gt; setError(true); 
 $ _response-&gt; set 消息($ this-&gt; __('无法移除项目。')); 
 $ _response-&gt; send(); 
 Mage :: logException($ e); 
} 
} 
 $  _response = Mage :: getModel('ajaxcart / response'); 
 $ _response-&gt; setMessage($ this-&gt; __('Item was removed。')); 
 //追加更新的块
 $ this  - &gt; getLayout() - &gt; getUpdate() - &gt; addHandle('ajaxcart'); 
 $ this-&gt; loadLayout(); 
 $ _response-&gt; addUpdatedBlocks($ _ response); 
 $ _response  - &gt; send(); 
} 
   
 
 

如果能得到一些帮助,我将非常感谢。 提前致谢。

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

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);    
                });    
            });    
        }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题