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

[5.x]: qty in populateLineItem event is always 1 #3883

Open
jornwildenbeest opened this issue Feb 5, 2025 · 3 comments
Open

[5.x]: qty in populateLineItem event is always 1 #3883

jornwildenbeest opened this issue Feb 5, 2025 · 3 comments

Comments

@jornwildenbeest
Copy link

What happened?

Description

Not sure if this is a bug or just changed in Craft Commerce 5.

But the qty in the populateLineItem event (https://craftcms.com/docs/commerce/5.x/extend/events.html#populatelineitem) is always 1.
We used the populateLineItem to calculate a new salePrice depending on how many items the customer bought.

Since updating from Craft 3 tot Craft 5 I've noticed that this new salePrice is wrong, so after a little investigating I found out that the qty in the event is always 1.

Probably because the default is set to 1: https://github.com/craftcms/commerce/blob/5.3.1/src/services/LineItems.php#L432

If this is just changed and not a bug anymore, which event do we need to use to calculate a new salePrice depending on custom Logic?

Steps to reproduce

  1. Add the populateLineItem in a module and log the qty.
  2. Add a product to the cart with qty higher then.
  3. The log shows that the qty is always 1, even when you add for example 5 products.

Expected behavior

The qty in the populateLineItem is greater then 1.

Actual behavior

The log shows that the qty is always 1, even when you add for example 5 products.

Craft CMS version

5.6.4

Craft Commerce version

5.3.1

PHP version

8.3

Operating system and version

Mac os

Database type and version

Mysql 8

Image driver and version

No response

Installed plugins and versions

Copy link

linear bot commented Feb 5, 2025

PT-2419 [5.x]:

@jornwildenbeest jornwildenbeest changed the title [5.x]: [5.x]: qty in populateLineItem event is always 1 Feb 5, 2025
@lukeholder
Copy link
Member

lukeholder commented Feb 6, 2025

@jornwildenbeest Thanks for reporting this. I have made a PR with some fixes to address the issues you were seeing. I will update you once it's in a release. Thanks!

@lukeholder
Copy link
Member

@jornwildenbeest We have decided to make that PR referenced above a part of larger changes we will be making in the next minor release (5.4) - including possible deprecations of those methods that need refactoring.

In the meantime, we think that it would be more accurate for you to use the

craft\commerce\elements\Order::EVENT_BEFORE_LINE_ITEMS_REFRESHED and \craft\commerce\elements\Order::EVENT_AFTER_LINE_ITEMS_REFRESHED events.

These events will be called when the order recalculates, and will have the correct qty when those events are triggered. You can still manipulate the line item the same as LineItems::EVENT_POPULATE_LINE_ITEM.

We will look to improve the upgrade guide to recommend these new events.

@lukeholder lukeholder reopened this Feb 6, 2025
@lukeholder lukeholder assigned lukeholder and unassigned nfourtythree Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants