0

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.

0

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.

  • 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 Jul 25 '18 at 7:19
0

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();
}

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.