1

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) {
        $item->getQuote()->removeItem($item->getId());
        $message = 'You must enter a value lower than $'.$max;

        //todo: could try _dataSaveAllowed

        Mage::throwException($message);
        return;
    }

    if(!is_null($new_price) && $this->isCurrency($new_price)) {
        $item->setCustomPrice($new_price);
        $item->setOriginalCustomPrice($new_price);
        $item->getProduct()->setIsSuperMode(true);
    }
}

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

3 Answers 3

3

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.

1
  • 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
    Commented Sep 9, 2015 at 5:32
1

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


    $item = $observer->getQuoteItem();
    if($item->getParentItem())
    {
        $item=$item->getParentItem();
    }
    else
    {
        $item=$item;
    }                      

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

     //set price
     $item->setCustomPrice($price);
     $item->setOriginalCustomPrice($price);
     $item->getProduct()->setIsSuperMode(true); 

 

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.

1
  • 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
    Commented Sep 9, 2015 at 21:15
1

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:

http://cloud.plugin.company/screenshots/Screenshot_at_02-33-12.png

http://cloud.plugin.company/screenshots/Screenshot_at_02-33-33.png

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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