Friday , July 29 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

--- Updated News ---

Here we go with the new generation of Magento 2 extensions, after its official release on 17 November 2015. Within the next technology of Magento 2, we expect better extensions for our E-commerce website, including of Magento 2 One Step Checkout, Magento 2 Gift Card and Magento 2 Store Locator. Having all these 3 products will help your store rocket the sales.

magento 2 one step checkout

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>