Programmatically add product to cart with price change Programmatically add product to cart with price change php php

Programmatically add product to cart with price change


After digging a bit into Magento's core code, I found that you need to use $item->getProduct()->setIsSuperMode(true) in order to make $item->setCustomPrice() and $item->setOriginalPrice() work.

Here is some sample code you can use within an Observer that listens for the checkout_cart_product_add_after or checkout_cart_update_items_after events. The code is logically the same except checkout_cart_product_add_after is called for only one item and checkout_cart_update_items_after is called for all items in the cart. This code is separated/duplicated into 2 methods only as an example.

Event: checkout_cart_product_add_after

/** * @param Varien_Event_Observer $observer */public function applyDiscount(Varien_Event_Observer $observer){    /* @var $item Mage_Sales_Model_Quote_Item */    $item = $observer->getQuoteItem();    if ($item->getParentItem()) {        $item = $item->getParentItem();    }    // Discounted 25% off    $percentDiscount = 0.25;     // This makes sure the discount isn't applied over and over when refreshing    $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);    // Make sure we don't have a negative    if ($specialPrice > 0) {        $item->setCustomPrice($specialPrice);        $item->setOriginalCustomPrice($specialPrice);        $item->getProduct()->setIsSuperMode(true);    }}

Event: checkout_cart_update_items_after

/** * @param Varien_Event_Observer $observer */public function applyDiscounts(Varien_Event_Observer $observer){    foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* @var $item Mage_Sales_Model_Quote_Item */) {         if ($item->getParentItem()) {             $item = $item->getParentItem();         }         // Discounted 25% off         $percentDiscount = 0.25;          // This makes sure the discount isn't applied over and over when refreshing         $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);         // Make sure we don't have a negative         if ($specialPrice > 0) {             $item->setCustomPrice($specialPrice);             $item->setOriginalCustomPrice($specialPrice);             $item->getProduct()->setIsSuperMode(true);         }    }}


Magento have changed the way the prices are calculated in the cart which makes it very difficult to do this in v1.4 onwards. If you do set the price using an Observer or other device, it will almost certainly be overwritten back to the catalog price.

Effectively, you need to use Shopping Cart rules to implement this.


It is possible to set a customer specific price of a quote item. Hence, something like this should do it:

$quoteItem = $quote->addProduct($product, $qty);$quoteItem->setCustomPrice($price);// we need this since Magento 1.4$quoteItem->setOriginalCustomPrice($price);$quote->save();

Hope this helps...