0

I update the product price (.price-box) on the product page via some custom JS component. When putting the product into cart it still has the old value from before the update.

How can I put the product into cart with the new price without actually saving the product with the new price but keeping it in cart and checkout?

1
  • Dava, Did you write a function? May 15, 2018 at 17:42

1 Answer 1

0

Assuming this is Magento 1.x if not let me know and I'll provide M2 version

You need to do this by hooking into the add to cart action using the observer and do something similar to this

private function replaxePrice($product_id,$newPrice){

$quote = Mage::getSingleton('checkout/session')->getQuote(); 

foreach ($quote->getAllVisibleItems() as $item) {
if($item->getProduct()->getId() != $product_id) continue; 

if ($newPrice > 0) { 
$item->setCustomPrice($newPrice);

$item->setOriginalCustomPrice($newPrice);
 $item->getProduct()->setIsSuperMode(true); 
} 
}
 $quote->save(); 
}

Sorry about poor formatting. Only certain thing I can do on mobile

Why reinvent the wheel, I has someone ask me this recently and I sent them this reference

Updated for M2

https://webkul.com/blog/magento2-set-custom-price-of-product/

Hope this resolved your issues

7
  • This looks like some M1 snippet, I am on M2 ;-) Furthermore how should I get the price from my Frontend JS component? Doesn't sound like a viable solution to be honest
    – codedge
    Jun 21, 2017 at 21:54
  • Ahh.. sorry - I disnt read your updated version.. yes M2
    – codedge
    Jun 21, 2017 at 21:55
  • Same principle really you just need to hook into the add to cart event and update price similar to M1 Jun 21, 2017 at 21:57
  • But the new price is calculated only in JS. How shall I pass it to the event?
    – codedge
    Jun 21, 2017 at 21:58
  • Iv updated my post that should point u in right direction Jun 21, 2017 at 22:00

Your Answer

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

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