Programmatically add product to cart with price change

前端 未结 6 1332
孤街浪徒
孤街浪徒 2020-11-28 20:44

I want to add a product to cart programmatically. Also, I want to change the product price when added to cart.

Suppose, my product\'s price is $100. I wanted to cha

6条回答
  •  半阙折子戏
    2020-11-28 21:05

    If I have to share my solution that I made on the base of Simon then I have managed to rewrite model class save function of quote.

    public function save()
    {
    
        $this->getQuote()->getBillingAddress();
        $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
        $this->getQuote()->collectTotals();
        //$this->getQuote()->save();
    
        foreach($this->getQuote()->getAllItems() as $item) {             
              $productId = $item->getProductId();
              $product = Mage::getModel('catalog/product')->load($productId);
              if($product->getAttributeText('is_dummy') == 'Yes') {
                $price = 2;
                $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 chenges with cart items.
         */
        Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));
        return $this;
    }
    

提交回复
热议问题