Magento – Add Product to Cart with Custom Price

cartcustompriceproduct

I want to add a "custom Price" which have been generated on the Product-Site to the product Price on the cart.

With the actual code it is only the value (price) of the hidden field which is delivered to the cart – without the product price.

Code with checkout_cart_product_add_after:

public function change_price(Varient_Event_Observer $observer) {



    $item = $observer->getQuoteItem();

    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    $newpricetest = Mage::app()->getRequest()->getPost('priceInput');

    $new_price = $item->getOriginalPrice() + $newpricetest;

    $item->setCustomPrice($new_price);
    $item->setOriginalCustomPrice($new_price);
    $item->getProduct()->setIsSuperMode(true);

}

What's wrong?

Best Answer

Using sales_quote_add_item event observer is working for me.

config.xml

<frontend>
    <events>
        <sales_quote_add_item>
            <observers>
                <mymodule_observer>
                    <type>singleton</type>
                    <class>MyCompany_MyModule_Model_Observer</class>
                    <method>updateCartPrice</method>
                </mymodule_observer>
            </observers>
        </sales_quote_add_item>
    </events>
</frontend> 

Observer.php

class MyCompany_MyModule_Model_Observer
{
    public function updateCartPrice(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();        
        $quoteItem = $event->getQuoteItem();
        //$product = $item->getProduct();        

        $customPrice = YOUR_CUSTOM_PRICE;
        $quoteItem->setOriginalCustomPrice($customPrice);
        $quoteItem->save(); 
    }    
}