1

I added an event subscriber for add to cart event. There I defined a function to react on add to cart event. Everything works fine as my eventsubscriber is being called. I want to update the unit price of product which is being added to cart. Below is the code I used but it is not working. Price of the product is not updating to the price I am setting.

public function updateProductPrice(CartEntityAddEvent $event) {
    $unit_price = new Price('15.00', 'EUR');
    $event->getOrderItem()->setUnitPrice($unit_price,TRUE);    
  }

I have tried with both overide TRUE and FALSE but nothing works.

2 Answers 2

1

The problem with this approach is the price will be recalculated on the next order item refresh. You can't just depend on altering this item at the Add to Cart. You'll need a price resolver or else some other tie into the order refresh process.

2
  • 1
    I managed to solve it by updating the price of order item rather than updating the orderitem of the event. I will put the code which I used. Let me know if that is an acceptable process.
    – Bharat
    Commented Jul 25, 2018 at 7:19
  • I think this may be possible now because order refresh doesn't touch order items which have unit prices overriden.
    – duru
    Commented Apr 17, 2020 at 3:18
1

I managed to update the price by updating the order item of order rather than the order item in event.

$items = $event->getOrder()->getItems();
foreach ($items as $key => $item) {
    $currencyCode = $item->getUnitPrice()->getCurrencyCode();
    $unit_price = new Price('20.00', $currencyCode);
    $item->setUnitPrice($unit_price,TRUE);
    $item->save();
}
2

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.