3

I'd like have a product that is basically a calculator, which I will build in Javascript. I want the add to cart process to grab the generated price from the page and submit it to the cart - which is as far as I have got.

I've created an observer to hook into the checkout_cart_product_add_after event and update the quote item price based on a field value in the submitted form, which works.

The problem I have is that, if you add a second or multiple versions of the item with different prices, it updates all the other versions in the cart to the same price - so you can never have multiples of the same item in the cart with different prices.

Anyone have any ideas? Here's the code in my observer:

public function modifyPrice(Varien_Event_Observer $observer) {
    $customprice = $_POST["customprice"];       
    $item = $observer->getQuoteItem();
    $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
    if ($customprice > 0) {
        $item->setCustomPrice($customprice);
        $item->setOriginalCustomPrice($customprice);
        $item->getProduct()->setIsSuperMode(true);
    }
}
user2565123
  • 293
  • 1
  • 5
  • 12
  • I'd suggest programmatically adding different additional options to the product you are adding to the cart. Magento will then treat those as different quote items rather than adding them together. You could then use JS to remove those options from the cart DOM so they are not visible – McNab Jul 11 '13 at 09:08
  • That's definitely an idea, I'll give it a whirl and get back to you. Thanks – user2565123 Jul 11 '13 at 09:11
  • Check the below link, this may helps you. http://stackoverflow.com/questions/9721583/changing-the-price-in-quote-while-adding-product-to-cart-magento – Sankar Subburaj Jul 11 '13 at 10:00
  • So I decided to do this properly and replicate the frontend JS price generator on the backend. So, I used the method detailed in the link below, which should give each cart item it's own options, which will actually properly relate to options selected on the product page - even better! http://stackoverflow.com/questions/9412074/magento-quote-order-product-item-attribute-based-on-user-input/9496266#9496266 I haven't fully tested yet but hoping the prices won't get overwritten as they are different quote items with different options. Sankar, thank you for the link. I'll update shortly. – user2565123 Jul 11 '13 at 11:13
  • I've resolved this issue. Will update the thread once my restrictions as a new user are lifted – user2565123 Jul 11 '13 at 11:29

2 Answers2

0

I think the only way to get this working is to dynamically create a new product with a new price or you try to work with attributes which replaces the price, whereas you have to code this logic into your shopping cart as well as in your checkout.

MageZeus
  • 184
  • 1
  • 6
0

Thank you to McNab, I followed the route of adding custom options to each item added to the cart to overcome the problem with it overwriting the price - which worked perfectly.

Furthermore, I decided to actually grab the options selected on the product page that the JS uses to generate the custom price, and replicate that price generation on the back end based on the selected options. Using the following instructions to add the custom options to the product on the fly, before it's passed to the cart:

Magento - Quote/order product item attribute based on user input

This makes the price calculation much more secure, the selected options are then shown in the cart and order details, and there's no price overwriting issue!

My first question on Stack Overflow and it's hell of a community, thank you all.

Community
  • 1
  • 1
user2565123
  • 293
  • 1
  • 5
  • 12