Magento – Dynamically calculated prices save after add to cart

cartproduct

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.

Best Answer

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