删除1量产品Magento

时间:2013-06-07 11:09:32

标签: ajax magento cart

我正在实施Magento的自定义侧边栏购物车,它将以此购物车中的产品为增量和减量,在ajax中刷新。

AJAX可以与HTML一起使用,因为返回值是整车,所以不能正常工作。

我使用CartController添加1件产品:/ checkout / cart / add / uenc / aHR0cDovL3BsYXkudGhlaGFwcHlwZWFyLm5ldC9zaG9wL2RyaW5rcw ,, / product / 586 /

是否可以只删除1个数量的产品?我是否必须在CartController中创建新的自定义函数?

谢谢, 亚当


解答:

您可以使用

调用此链接

/ checkout / cart / remove / id / $ ITEMID / uenc / aHR0cDovL3BsYXkudGhlaGFwcHlwZWFyLm5ldC9zaG9w /(取决于您的magento设置链接可能不同)

复制到CartController.php

public function removeAction()
    {
        $cart   = $this->_getCart();
        $params = $this->getRequest()->getParams();



        try {
            if (isset($params['qty'])) {
                $filter = new Zend_Filter_LocalizedToNormalized(
                    array('locale' => Mage::app()->getLocale()->getLocaleCode())
                );
                $params['qty'] = $filter->filter($params['qty']);
            }

            $product = $this->_initProduct();
            $related = $this->getRequest()->getParam('related_product');

            /**
             * Check product availability
             */

            $id = (int) $this->getRequest()->getParam('id');

            $items = $cart->getItems();
            foreach ($items as $item) {
                if ($item->getProduct()->getId() == $id) {
                if( $item->getQty() == 1 ){
                    $cart->removeItem($item->getItemId())->save();
                }
                else if($item->getQty() > 1){
                    $item->setQty($item->getQty() - 1);
                    $cart->save();
                }
                break;
                }
            }

            $this->_getSession()->setCartWasUpdated(true);

            /**
             * @todo remove wishlist observer processAddToCart
             */
            Mage::dispatchEvent('checkout_cart_add_product_complete',
                array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
            );

            if (!$this->_getSession()->getNoCartRedirect(true)) {
                if (!$cart->getQuote()->getHasError()){
                    //$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
                    //$this->_getSession()->addSuccess($message);
                }
                $this->_goBack();
            }
        } catch (Mage_Core_Exception $e) {
            if ($this->_getSession()->getUseNotice(true)) {
                $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
            } else {
                $messages = array_unique(explode("\n", $e->getMessage()));
                foreach ($messages as $message) {
                    $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
                }
            }

            $url = $this->_getSession()->getRedirectUrl(true);
            if ($url) {
                $this->getResponse()->setRedirect($url);
            } else {
                $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
            }
        } catch (Exception $e) {
            $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
            Mage::logException($e);
            $this->_goBack();
        }
    }   

0 个答案:

没有答案