magento二次开发查看购物车界面删除商品弹出提示框,但是无论确认或取消都会删除

小小个 发布于 2015/05/28 10:30
阅读 622
收藏 0

magento二次开发,查看购物车界面删除商品时添加弹出提示框,但是无论确认或取消都会删除产品

代码修改为:

  <td><a id="<?php echo $_item->getId(); ?>"
           href="<?php echo $this->getDeleteUrl()?>"
            onclick="return confirm("你确认要删除吗?");"
           title="<?php echo $this->__('Remove item')?>"
           class="remove-btn"><span class="icon-remove"></span></a></td>

求大神指导

加载中
0
houhuiliang
houhuiliang

回复 @小小个 : 把链接去掉就好了呀,onclick事件定义一个函数,onclick='deleteItem()'


<a onclick="deleteItem()" title="<?php echo $this->__('Remove item')?>"
           class="remove-btn"><span class="icon-remove"></span></a>



<script>
function deleteItem(){
      if(confirm('delete it ?')){
         window.location.href="<?php echo $this->getDeleteUrl()?>";
      }
}

</script>




小小个
这个我也试过,但问题是当我点击确认删除后会到达http://localhost/magento1910/checkout/cart/delete/id/2592/uenc/aHR0cDovL2xvY2FsaG9zdC9tYWdlbnRvMTkxMC9jaGVja291dC9jYXJ0Lw,,/这个网址,显示的是乱码,当我返回网页的时候他就去到了购物车界面了,而且也删除了商品
0
houhuiliang
houhuiliang
href="<?php echo $this->getDeleteUrl()?>"
            onclick="return confirm("你确认要删除吗?");"
           title="<?php echo $this->__('Remove item')?>"
           class="remove-btn"><span class="icon-remove"></span></a>
这个没用啊,你点击过之后不管是确认还是取消,都会跳转到删除页面href="<?php echo $this->getDeleteUrl()?>",删除后又重定向到购物车页。
小小个
是呀,所以无论是确认还是取消都会删除的,你有解决的办法吗?
0
houhuiliang
houhuiliang
/**
     * Delete shoping cart item action
     */
    public function deleteAction()
    {
        $id = (int) $this->getRequest()->getParam('id');
        if ($id) {
            try {
                $this->_getCart()->removeItem($id)
                  ->save();
            } catch (Exception $e) {
                $this->_getSession()->addError($this->__('Cannot remove the item.'));
                Mage::logException($e);
            }
        }
        $this->_redirectReferer(Mage::getUrl('*/*'));
    }

不会有乱码啊,会自动跳转

小小个
我觉得是因为我装了一个小型购物车插件Ajaxminicart影响到了,它重写了magento原有的checkout模块
0
小小个

引用来自“houhuiliang”的评论

/**
     * Delete shoping cart item action
     */
    public function deleteAction()
    {
        $id = (int) $this->getRequest()->getParam('id');
        if ($id) {
            try {
                $this->_getCart()->removeItem($id)
                  ->save();
            } catch (Exception $e) {
                $this->_getSession()->addError($this->__('Cannot remove the item.'));
                Mage::logException($e);
            }
        }
        $this->_redirectReferer(Mage::getUrl('*/*'));
    }

不会有乱码啊,会自动跳转

配置文件config.xml中 
<rewrite>
            <Datta_ajaxminicart_checkout_cart>
                <from><![CDATA[#^/checkout/cart/#]]></from>
                <to>/ajaxminicart/checkout_cart/</to>
            </Datta_ajaxminicart_checkout_cart>
        </rewrite>
然后它好像修改了原有magento里面的deleteAction()方法
/**
     * Delete shoping cart item action
     */
    public function deleteAction() {
        $id = (int) $this->getRequest()->getParam('id');     
        if ($id) {
            try {
                $this->_getCart()->removeItem($id)
                        ->save();  
                 
            } catch (Exception $e) {
                $_response = Mage::getModel('ajaxminicart/response');
                $_response->setError(true);
                $_response->setMessage($this->__('Error...Cannot remove the item.'));
                $_response->send();
                Mage::logException($e);
            }
        }  
         Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
        $quote = Mage::getSingleton('checkout/cart')->getQuote(); 
        $shippingTaxamount = Mage::helper('checkout')->getQuote()->getShippingAddress()->getData('tax_amount'); 
        // get coupon discounted value            
        $totals = $quote->getTotals(); //Total object
        if(isset($totals['discount']) && $totals['discount']->getValue()) {
            $discount = Mage::helper('core')->currency($totals['discount']->getValue()); //Discount value if applied
        }else{
            $discount ='';
        } 
        //get discount value end                
        $count = $quote->getItemsCount();  
        $grandTotal = $quote->getGrandTotal();
        $subTotal = $quote->getSubtotal();
        $_response = Mage::getModel('ajaxminicart/response');
        $_response->setCarttotal($grandTotal); 
        $_response->setCartsubtotal($subTotal); 
        $_response->setDiscount($discount);           
        $_response->setShippingtaxamount($shippingTaxamount);
        $_response->setCartcount($count);
        $_response->setMessage($this->__('Item was removed'));
        //append updated blocks
        $this->getLayout()->getUpdate()->addHandle('ajaxminicart');
        $this->loadLayout();
        $_response->addUpdatedBlocks($_response);
        $_response->send();
    }
小小个
回复 @houhuiliang : 谢谢你了,我把它重写的deleteAction()给注释掉就可以了!也走了一遍流程好像没发现会影响到其他功能!真的谢谢你的积极帮助!
houhuiliang
houhuiliang
在这个地方按照你的需要修改下就好了
返回顶部
顶部