1

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

0 に答える 0