Monday , May 30 2016
Home >> All tutorial >> How to Programmatically Add Product to Cart with Price Change in Magento

How to Programmatically Add Product to Cart with Price Change in Magento

How to Programmatically Add Product to Cart with Price Change in Magento

Use the following code to programmatically add product to cart with price change:

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); 
 } 
} 
}

 

Recommended readingTop 5 Best magento hosting

--- Latest News ---

Wanna save your time for importing & exporting data between multiple systems?

Check this extension: magento salesforce integration. It will automate all your tedious tasks by connecting & transferring data between Magento & Salesforce. Managing all data in 1 place is a piece of cake now.

magento salesforce

Plus, the new magento pos ipad app is realesed, demo it now.

.

About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>