Tutorial on how to Programmatically Add Product to Cart with Price Change in Magento
Magento Content management System is an easy to use and flexible eCommerce CMS. You can customize the settings for your online store in simple steps. This blog post will cover one such customization for Magento store managers – how to programmatically Add Product to Cart with Price Change.
But before we begin with the tutorial let us first know why some store managers need to do this. Let for instance, the price of a product is $10, but he/she wants to change it to $9. So, how to change it programmatically without giving discounts or changing it manually?
How to Programmatically Add Product to Cart with Price Change in Magento
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.
Use the following code to programmatically add product to cart with price change (put it into Observe.php):
Event: checkout_cart_product_add_after
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * @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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/** * @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); } } } |
This will surely help you to programmatically add product to cart with price change. But, if you unsuccessfully in changing it, then request a quote. Our Magento team will provide you expert guidance for detecting and solving the problems.
6 Comments
vikas
Hi,
I don’t know php.
I want to create the products programmatically in magento. everyone gave php code. no one mentioned in steps.
1)where and in which folder I have to add the code.
2)Is I have to save in .php file.
3)Is there anything i have to add in app/code/core/Mage/catalog/php
Thanks
Josh
Hi Vikas
You can insert the code in the post into Observer.php
bijay
how to insert another custom price with custom calculations in magento cart from products listing page to cart page
Matt
I’m pretty new to Magento, but why not get the original price from the product rather than $item->getOriginalPrice(). The $item->getOriginalPrice() has been returning 0 a lot of the time, I’m not sure why. Maybe an update changed it? I changed your code into:
$product = $item->getProduct();
$specialPrice = $product->getPrice() – ($product->getPrice() * $percentDiscount);
Matt
Oh, I forgot to say thank you for this post! It saved me some time trying to figure out how to have certain products use a special pricing calculator that Magento doesn’t really support other than to change it with the two events that you wrote about!
sdg
Hi,
I don’t know php.
I want to create the products programmatically in magento. everyone gave php code. no one mentioned in steps.
1)where and in which folder I have to add the code.
2)Is I have to save in .php file.
3)Is there anything i have to add in app/code/core/Mage/catalog/php
Thanks