Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x]: EVENT_POPULATE_LINE_ITEM fires multiple times #3346

Closed
pixelmachine opened this issue Dec 2, 2023 · 1 comment
Closed

[4.x]: EVENT_POPULATE_LINE_ITEM fires multiple times #3346

pixelmachine opened this issue Dec 2, 2023 · 1 comment
Labels
bug commerce4 Issues related to Commerce v4

Comments

@pixelmachine
Copy link

pixelmachine commented Dec 2, 2023

What happened?

Description

I've been using EVENT_POPULATE_LINE_ITEM for a long time in Commerce 3 and I've recently upgraded to 4. However now EVENT_POPULATE_LINE_ITEM seems to fire at least 3 times when an item is added to the cart. Each is identical.

Steps to reproduce

  1. Add a module like this.
Event::on(
  LineItems::class,
  LineItems::EVENT_POPULATE_LINE_ITEM, 
  function(LineItemEvent $event) 
  {
     if ($event->isNew) 
     {
       Craft::info($isNew, 'my-module');
     }
  }
);
  1. Add an item to the cart.

Expected behavior

A single log entry is created

Actual behavior

Three log entries are created

2023-12-01 21:33:52 [web.INFO] [my-module] true {"memory":44893448} 
2023-12-01 21:33:52 [web.INFO] [my-module] true {"memory":45303680} 
2023-12-01 21:33:52 [web.INFO] [my-module] true {"memory":47529120} 

Craft CMS version

4.5.11.1

Craft Commerce version

4.3.2

Installed plugins and versions

Gift Voucher 3.0.9

@pixelmachine pixelmachine added commerce4 Issues related to Commerce v4 bug labels Dec 2, 2023
@lukeholder
Copy link
Member

We have reduced the number of recalculations in recent releases of Commerce, but it's possible for the event to fire multiple times for various reasons, like if the recalculation needs to run again after a shipping method that is selected becomes disqualified due to order value etc. So the line item could be identical each time.

You need to make sure your event handler is able to handle deciding when to modify the line item again or if can skip making the modifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug commerce4 Issues related to Commerce v4
Projects
None yet
Development

No branches or pull requests

2 participants