Wednesday, July 18, 2012

Add Product to Cart with Price Change

If you want to add a product to the cart after change its price, then you have to write an Observer that listens the checkout_cart_product_add_after or checkout_cart_update_items_after event. The code is 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.


/**
* @param Varien_Event_Observer $observer
*/
public function myDiscount(Varien_Event_Observer $observer)
{
$item = $observer->getQuoteItem();
if ($item->getParentItem()) {
$item = $item->getParentItem();
}

//discount 20% off
$discount = 0.20;

// Check if the discount isn't applied over and over while refreshing
$specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $discount);

if ($specialPrice > 0) {
$item->setCustomPrice($specialPrice);
$item->setOriginalCustomPrice($specialPrice);
$item->getProduct()->setIsSuperMode(true);
}
}

3 comments:

  1. Hello,

    Following Url is Great Help for Update Price Of Product In Cart In Magento ?

    http://www.webslike.com/Thread-How-To-Programmatically-Update-Price-Of-Product-In-Cart-In-Magento

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Great Read! I am impressed on how you make your article easy to understand. I'll come back for more :D

    Japs Buidon is a Social Media Specialist and belongs to a team of Magento Developer in Florida. For more tutorial and tips you can follow him here -> alwaysopencommerce.com

    ReplyDelete