Programmatically Add Product to Cart with Price Change in Magento

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

 Event: checkout_cart_update_items_after

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.


5 Responses to “Programmatically Add Product to Cart with Price Change in Magento”

  1. vikas
    March 24, 2014 at 8:39 am #


    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


    • Josh
      March 28, 2014 at 4:57 am #

      Hi Vikas
      You can insert the code in the post into Observer.php

  2. June 6, 2014 at 10:45 am #

    how to insert another custom price with custom calculations in magento cart from products listing page to cart page

  3. September 24, 2014 at 8:16 pm #

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

    • September 24, 2014 at 8:20 pm #

      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!

Leave a Comment