19

I want to change the product price while add that product to cart.

How its possible let me know...

Samsquanch
  • 8,866
  • 12
  • 50
  • 89
Sankar Subburaj
  • 4,992
  • 12
  • 48
  • 79

7 Answers7

40

The way to do it is add an observer which looks for this event 'sales_quote_add_item':

<events>
    <sales_quote_add_item>
        <observers>
            <priceupdate_observer>
                <type>singleton</type>
                <class>mymodule/observer</class>
                <method>updatePrice</method>
            </priceupdate_observer>
        </observers>
    </sales_quote_add_item>
</events>

The observer should have a method which does something like this:

public function updatePrice($observer) {
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();
    $new_price = <insert logic>
    $quote_item->setOriginalCustomPrice($new_price);
    $quote_item->save();
}
Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
Gershon Herczeg
  • 3,016
  • 23
  • 33
  • 1
    thanks for sharing info! So, the XML would go into a local module that we need to create, yes? (I found this by searching: http://goo.gl/UBFgS) And then can the public function be declared in the phtml that is running the cart code I'm contemplating? – Krista K May 08 '12 at 21:43
  • 1
    Here this should give you a overview on how to create an observer : http://alturl.com/tja43 – Gershon Herczeg May 08 '12 at 22:02
  • Hi, If I add configurable product with this modification, it adds two products one is configurable and the other is simple. How to solve this? – Dushyant Joshi Jul 02 '13 at 11:30
  • So does this alter price also appear in checkout and in invoice ? Thanks – numerical25 Feb 16 '14 at 20:10
  • If the product has custom options and the price of a single option needs to be changed, does the logic need to simply update the option price, or does it need to have a running total of all custom options and then set that running total to the quote item? – pspahn Dec 10 '14 at 20:45
  • 1
    @GershonHerczeg I too can confirm that on my magento version doing `$quote_item->save();` have unwanted behaviour. Since your question is the accepted one, can you maybe add a note to the answer saying, that in some case you have to remove this `save` ? – β.εηοιτ.βε Jun 08 '15 at 10:49
4

You can use an observer class to listen to checkout_cart_product_add_after, and use a product’s “Super Mode” to set custom prices against the quote item.

In your /app/code/local/{namespace}/{yourmodule}/etc/config.xml:

<config>
    ...
    <frontend>
        ...
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <unique_event_name>
                        <class>{{modulename}}/observer</class>
                        <method>modifyPrice</method>
                    </unique_event_name>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        ...
    </frontend>
    ...
</config>

And then create an Observer class at /app/code/local/{namespace}/{yourmodule}/Model/Observer.php

    <?php
        class <namespace>_<modulename>_Model_Observer
        {
            public function modifyPrice(Varien_Event_Observer $obs)
            {
                $customPrice = Mage::getSingleton(’core/session’)->getCustomPriceCalcuation(); // Provide you price i have set with session
                $p = $obs->getQuoteItem();
                $p->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice); 
            }

        }
Xman Classical
  • 5,179
  • 1
  • 26
  • 26
3

The issue regarding configurable product has been solved. You simply remove the

$quote_item->save();

and then for the product will not added to the cart twice. But one another very serious issue is remaining in this function. That is, with this function we can update the item price in cart but after added to cart, product price are not changed according the different currency. So this function can not be used for a store having multiple currency.

If anyone find any solution on that issue, please share with us...

3

Soup to Nuts.

File: /app/etc/modules/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Ajax_ProductAdjust>
      <codePool>local</codePool>
      <active>true</active>
    </Ajax_ProductAdjust>
  </modules>
</config>

File: /app/code/local/Ajax/ProductAdjust/etc/config.xml

<?xml version="1.0"?>
      <config>
       <modules>
         <Ajax_ProductAdjust>
           <version>1.0.1</version>
         </Ajax_ProductAdjust>
       </modules>
       <global>
           <models>
             <Ajax_ProductAdjust>
               <class>Ajax_ProductAdjust_Model</class>
             </Ajax_ProductAdjust>
           </models>
           <events>
              <sales_quote_add_item>
                  <observers>
                     <ajax_productadjust_model_observer>
                        <type>singleton</type>
                        <class>Ajax_ProductAdjust_Model_Observer</class>
                        <method>updatePrice</method>
                     </ajax_productadjust_model_observer>
                 </observers>
              </sales_quote_add_item>
          </events>
      </global>
     </config>

File: /app/code/local/Ajax/ProductAdjust/Model/Observer.php

<?php
//Notes
class Ajax_ProductAdjust_Model_Observer
{

    public function _construct()
      {
      }

    public function getNewPrice()
      {
        //Your new functionality here
        //
        $newprice = "";

        return $newprice;
      }

     public function updatePrice( Varien_Event_Observer $observer ) 
     {
        $event = $observer->getEvent();
        $quote_item = $event->getQuoteItem();
        $new_price = $this->getNewPrice();
        $quote_item->setOriginalCustomPrice($new_price);
        $quote_item->save();
      }
 }

Cheers,

JoshBelke
  • 206
  • 1
  • 5
2

As righty answered by Gershon Herczeg, Jürgen Thelen and Xman Classical. You will need to write a observer of sales_quote_add_item event. When any product is added to cart your observer will be triggered. If the product is Configurable then this event will be fired twice, You will have to something like that to get simple product only.

    $item = $observer->getEvent()->getQuoteItem();
    $quote = $item->getQuote();
    $product = $item->getProduct();
    if ($product->getTypeId() != "configurable") {
       //Do your thing here
    }
1

The question does not state whether this should be done by adding some logic to the code or not. So since you have answers for developer there is also something that is called cart price rules (in admin panel go to Promotions > Shopping Cart Price Rules) where you can create different rules for making sales and discounts (with or without coupons).

Zefiryn
  • 2,240
  • 2
  • 20
  • 30
0
    To change product price while adding product to cart, use an observer event.
    Follow the steps given below
    1. Add an observer in your module config.xml file.
    2. Create an observer file in your model
    3. add checkout_cart_product_add_after event

File: app/code/local/Namespace/Module/etc/config.xml

eg: app/code/local/MGS/Rileytheme/etc/config.xml

     <frontend>
           <routers>
              <routeurfrontend>
                  <use>standard</use>
                  <args>
                     <module>MGS_Rileytheme</module>
                     <frontName>rileytheme</frontName>
                  </args>
               </routeurfrontend>
           </routers>
           <layout>
            <updates>
             <rileytheme>
              <file>rileytheme.xml</file>
             </rileytheme>
            </updates>
           </layout>
            <events>
              <checkout_cart_product_add_after>
                <observers>
                    <rileytheme>
                    <class>rileytheme/observer</class>
                    <method>modifyPrice</method>
                    </rileytheme>
                </observers>
               </checkout_cart_product_add_after>
            </events></b>
     <frontend>

File:app/code/local/MGS/Rileytheme/Model/Observer.php

class MGS_Rileytheme_Model_Observer {
    public function modifyPrice(Varien_Event_Observer $observer) {
         //$order = Mage::registry('current_order'); For getting current order 
         //$orderid = $order->getRealOrderId(); For getting orderid
          $quote_item = $observer->getQuoteItem();
          $payment = 30; //add your custom product price here and do your logic here
          $quote_item->setOriginalCustomPrice($payment);
          $quote_item->save();
          return $this;
    }
 }
suruthi
  • 31
  • 1