More than 800+ satisfied clients and counting, since 2010

Blog

Add Products to Magento Cart with Price Change

Add Products to Magento Cart with Price ChangeMagento is an easy to use content management system with an unique blend of user friendly and advanced features. Using this eCommerce CMS you can be able to add products to Magento cart with price change. This blog post covers how you can do this by the help of functional codes.

How to Add Products to Magento Cart with Price Change

Use the following code to add products to Magento Cart with price change:

$item->getProduct()->setIsSuperMode(true) in order to make $item->setCustomPrice() and $item->setOriginalPrice()

You can also use the code given below to add products to Magento Cart with price change:

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

Sample to test programmatically addition of products with slight price change in Magento shopping carts:

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

If you face any sort of difficulty to add products to Magento cart with price change, then request a quote today. Our team of web developers will assist you in the process. We also offer Magento Upgrade, Magento installation and Magento SEO services as well. Check our stores for Magento custom extensions.

Written by

The author didnt add any Information to his profile yet