I currently have an observer which watches for when a product is added to the shopping cart (checkout_cart_product_add_after) and applies a custom price from the product view page. This works fine but I need for it to add a separate version of the same product to the cart if the price is different from any of the current ones. E.g. so the user can buy 1x $400 item, 2x $300 item.

Can this be done ? Currently using a simple product as nothing varies other than the price.

Here is my observer function

public function applyCustomPrice($observer)

    $item = $observer->getQuoteItem();

    if ($item->getParentItem()) {
        $item = $item->getParentItem();

    $product = $observer->getProduct();
    $max = $product->getMaxprice();
    if($max) {
        $max = $max * 1;
    } else {
        $max = 500;

    $new_price = Mage::app()->getRequest()->getPost('priceInput');

    if($new_price > $max) {
        $message = 'You must enter a value lower than $'.$max;

        //todo: could try _dataSaveAllowed


    if(!is_null($new_price) && $this->isCurrency($new_price)) {

function isCurrency($number)
    return preg_match("/^[0-9]+(?:\.[0-9]{1,2})?$/", $number);

This is not quite answering your question, but have you explored using sales rules instead of a coded solution? Go to the magento backend, click through to Promotions -> Shopping Cart Price Rules.

  • Problem is that the user can add any of the products with a value anywhere from $10 to $1000 so it needs to be flexible. Don't think the cart rules would do the job... unless I'm wrong?? – Onfire Sep 9 '15 at 5:32

your observer code looks about right to me, I had a similar requirement a while back and used this

    $item = $observer->getQuoteItem();

    $price=$this->getModifiedPrice(); // function to get the price

     //set price


Which was hooked to this event sales_quote_add_item, I may be mistaken but I think you are currently hooking into the wrong event.

  • I changed my observer to listen on sales_quote_add_item and the code still works ok for the first time adding the product to the cart (it applies the custom price) but then when I add the product again it doesn't seem to execute the code at all. I put a die() in my observer and nothing happens - it just adds the same product with the custom price from the first time adding it (I guess it just increases the qty by 1) – Onfire Sep 9 '15 at 21:15

You can use custom options for this.

Using custom options you can let users pick the price they want. Besides that they can order multiple products with different values for the custom options.

Please check the following screenshots:



Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.