2

We tried to update the product price after addto cart. I done the following code but cant get the item product price.

In app/etc/modules

 <?xml version="1.0"?>
<config>
  <modules>
    <test_Productoptions>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
       <depends>
            <Mage_Checkout />
       </depends>
    </test_Productoptions>
  </modules>
</config>

In app/code/local/test/productoptions/etc

0.0.1 Test_Productoptions_Model

        <checkout_cart_product_add_after>
            <observers>
                <priceupdate_observer>
                    <type>singleton</type>
                    <class>test_productoptions/observer</class>
                    <method>updatePrice</method>
                </priceupdate_observer>
            </observers>
        </checkout_cart_product_add_after>
    </events>
</frontend>

I Write the observer in app/code/local/test/productoptions/model

<?php
class Test_Productoptions_Model_Observer
{

    public function _construct()
      {
      }

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

        return $newprice;
      }

     public function updatePrice( Varien_Event_Observer $observer ) 
     {

        $quote_item  = $observer->getQuoteItem();
        $product     = $quote_item->getProduct();
        $quantity    = $product->getQty();

       echo $quote_item->getPrice();
      die();

     }
}

In this $quote_item->getPrice() is return null value. How to get the currently added product price from quote.

2 Answers 2

2

First set this code in config.xml

<checkout_cart_product_add_after>
    <observers>
        <custom_catalog_price_observer>
            <type>singleton</type>
            <class>productoptions/observer</class>
            <method>setProductCustomPrice</method>
        </custom_catalog_price_observer>
    </observers>
</checkout_cart_product_add_after>

You should use below method in Observer

public function setProductCustomPrice($observer)
{
    // Get the quote item
    $item = $observer->getQuoteItem();
    // Ensure we have the parent item, if it has one
    $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
    // Load the custom price

    $org_price = $item->getProduct()->getFinalPrice();

    //SET YOUR CUSTOM PRICE HERE
    $custom_price = $org_price + 15;

    $price = $custom_price;
    // Set the custom price
    $item->setCustomPrice($price);
    $item->setOriginalCustomPrice($price);
    // Enable super mode on the product.
    $item->getProduct()->setIsSuperMode(true);
}

Replace this line $custom_price = $org_price + 15; with your custom price logic

10
  • Hi, i cant add the product to cart. Show the error message as "Canot add to product in cart".
    – Rat
    Commented Apr 5, 2016 at 11:47
  • This is my function - public function updatePrice( Varien_Event_Observer $observer ) { $item = $observer->getQuoteItem(); $item = ( $item->getParentItem() ? $item->getParentItem() : $item ); $org_price = $item->getProduct()->getFinalPrice(); $custom_price = $org_price * $_REQUEST['cellcount']; // echo $quote_item->getPrice(); //$new_price = 2000*$_REQUEST['cellcount']; $item->setCustomPrice($custom_price); $item->setOriginalCustomPrice($custom_price); $item->save(); }
    – Rat
    Commented Apr 5, 2016 at 11:47
  • If not logged in customer, i got the message as "Cannot add the item to shopping cart". Once login , its work perfectly.
    – Rat
    Commented Apr 5, 2016 at 12:32
  • I think you are missing $item->getProduct()->setIsSuperMode(true); line. You need to use $item->getProduct()->setIsSuperMode(true); in order to make $item->setCustomPrice(); and $item->setOriginalCustomPrice($price); to work. Commented Apr 6, 2016 at 5:55
  • Hi, I added the $item->setCustomPrice($custom_price); $item->setOriginalCustomPrice($custom_price); $item->getProduct()->setIsSuperMode(true); $item->save(); But same error message show. This issue occured not logged customers only.
    – Rat
    Commented Apr 6, 2016 at 6:55
0

Use below mentioned event it's will work perfectly.

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

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

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

class Test_Productoptions_Model_Observer
{
public function updatePrice($observer) {
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();
    $new_price = 1254 // your price logic goes here
    $quote_item->setOriginalCustomPrice($new_price);
    $quote_item->save();
}
}

Here is the code to get subtotal and product price

$event = $observer->getQuote();
        $subTotal = $event->getSubtotal();
        $grandTotal = $event->getGrandTotal();

        foreach($event->getAllItems() as $quote_item) {
            echo $quote_item->getPrice();
        }
4
  • Hi, I need to some calculation based on currently added product. But i cant get the price from quote. I will try same code above, its working. But need to get the price from quote. $new_price = $quote_item->getPrice()*4
    – Rat
    Commented Mar 31, 2016 at 6:19
  • I have updated the answer see use given codes in updatePrice() function and do whatever you want
    – Arunendra
    Commented Mar 31, 2016 at 6:28
  • I want get the price from quote, but $quote_item->getPrice() is return null value. How to get the price from quote?
    – Rat
    Commented Mar 31, 2016 at 6:33
  • $quote_item->getPrice() it should return price for more see here stackoverflow.com/questions/15139697/…
    – Arunendra
    Commented Mar 31, 2016 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.