Magento – add product to cart and change price and weight

cartcheckoutevent-observer

I need a module that when you add product to cart check two conditions and if they are correct will change the price for the product in the basket.

I need do something like this:
– when product is added to the cart – get Qty and custom_attribute of product
– if qty<0 and cutom_attribute value for product is >0 change of product price in cart (the changed price should be use in transactional emails, order view, on the invoice etc.)

I try use checkout_product_add_after, but I cannot get product attribute and check condition. How to get custom product attribute in this event?

Maybe should I use another event?

Best Answer

Try adding an observer for event 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>

In your observer :

public function updatePrice($observer) {
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();

    $product = $quote_item->getProduct(); //You may need to load the product module or add the attribute to flat table
    $new_price = <insert logic>
    $quote_item->setOriginalCustomPrice($new_price);
    $quote_item->setWeight($newWeight);
    $quote_item->save();
}

See https://stackoverflow.com/questions/9721583/changing-the-price-in-quote-while-adding-product-to-cart-magento