Saturday, September 19, 2015

Product price change programatically after add to cart in magento

Step 1: Create an Attribute for the products
Example :-  Change Price Set its type yes/no for the product 


Step 2: Open File

code/core/mage/checkout/model/cart.php


Step 3: Goto on this function

 public function save()
    {
      ........
      ........
     }
Step 4: Copy and Paste below code,
public function save()
    {
        Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));

        $this->getQuote()->getBillingAddress();
        $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
        $this->getQuote()->collectTotals();
               
        foreach($this->getQuote()->getAllItems() as $item) {             
          $productId = $item->getProductId();
          $product = Mage::getModel('catalog/product')->load($productId);
          if($product->getAttributeText('change_price') == 'Yes')
         {
                 $price = 5;
                 $item->setCustomPrice($price);
                 $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;
      }
Step 5: Now, Insert Product  Admin side  and set Attribute Change Price value yes for that product
Step 6: Add that product  to cart you can see that, product  price will change to $5.

 Enjoy!!!!!

0 comments:

Post a Comment