Change Price in cart Programatically in magento.

Hi,

  Here for change the product price into cart programatically

  There is an simple way for change this product price and add custom price for the specific product into cart when add the product into cart in magento.

1) Create an Attribute for the products,

   Suppose an Ex. is Change Price
   Set its type yes/no for the product

2) Open File, code/core/mage/checkout/model/cart.php

3) Goto on this function,

     public function save()
    {
      ........
      ........
     }

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;
      }

5) Now, Insert Product form Admin and set its Change Price value yes for that product,

6) Add that product to cart you can see that, product Unit price will change to $5.

   This is work for me in 1.7.0.2

 Enjoy!!!!!

Comments

Post a Comment

Popular Posts

Magmi Single and Multiple Category Importer