简体   繁体   中英

Magento add to cart issue

I am facing problem in magento. When adding products to cart, it successfully adds one item but when I try to add another item it removes the previous item and adds the new item. This is because the quote id changes every time, with closing browser or anything else.

Any idea how to resolve this issue?

you can debug in this function

app\\code\\core\\Mage\\Checkout\\Model\\Cart.php

and find this function

 public function save()
{
    Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));

    $this->getQuote()->getBillingAddress();
    $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
    $this->getQuote()->collectTotals();
    $this->getQuote()->save();
    $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId());
    /**
     * Cart save usually called after changes with cart items.
     */
    Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));
    return $this;
}

you can debug here. hope this will help you.

I have edit this code for custom price

      public function save()
                          {
            Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));
        $this->getQuote()->getBillingAddress();
                    $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
                    $this->getQuote()->collectTotals();

                   // $this->getQuote()->save();
                    if(isset($_POST['product']))
                    $pid=$_POST['product'];

                    if(isset($_POST['npn']))
                    $new_price=$_POST['npn'];

                    foreach($this->getQuote()->getAllItems() as $item) {    
                        $productId = $item->getProductId();     
                        $product = Mage::getModel('catalog/product')->load($productId);
                        if(isset($pid))
                        {
                        if($productId==$pid)
                        {
                        if(isset($_POST['npn']) && $_POST['npn']!='')
                        {
                        $price = $_POST['npn'];
                        $item->setCustomPrice($price);
                        // we need this since Magento 1.4
                        $item->setOriginalCustomPrice($price);
                        }
                        }
                        }
                    }  
                    $this->getQuote()->save();
                    $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId());
                    /**
                     * Cart save usually called after changes with cart items.
                     */
                    Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));
                    return $this;
                }

hope it will help you !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM