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

Apply discounts to the ecommerce event if available #419

Merged

Conversation

ianlin
Copy link
Contributor

@ianlin ianlin commented May 9, 2024

Changes proposed in this Pull Request:

Closes #417. This PR fixes a bug that the price in purchase events does not take coupon discounts into account by getting the price after discount using the method get_total() in WC_Order_Item_Product class, and add the discount parameter to the ecommerce event.

Only the purchase event will include discounts if available, since only the method get_formatted_order() will include the "price after coupon discounts" for each order line item.

Checks:

  • Does your code follow the WordPress coding standards?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Screenshots:

Detailed test instructions:

  1. Checkout the branch of this PR and run npm ci and npm run build
  2. Open Network tab from browser dev console, add google-analytics as the filter
  3. Add some products into the cart, for example, two products.
  4. Go to checkout page
  5. Apply a coupon
  6. Complete the purchase, see the order received page
  7. From the Network tab, find the request with dt: Order Confirmation and en: purchase, which is the purchase event
  8. From this event you should see there are two product parameters starts with pr1 and pr2, e.g.
    • pr1: id28~nmSingle~caMusic~qt1~pr12~ds12
    • pr2: id36~nmBeanie with Logo~caAccessories~qt1~pr10~ds8
  9. The pr in the value is the price after coupon discounts, and ds is the amount of coupon discounts.
  10. Add another two products into the carts, complete the order WITHOUT applying any coupons.
  11. You should see there is no ds in the event parameters, and the price is just the regular price.

Additional details:

Changelog entry

Fix - Apply discounts to the ecommerce event if available

Right now only the purchase event will include discounts if available.
@ianlin ianlin requested a review from a team May 9, 2024 08:37
@ianlin ianlin self-assigned this May 9, 2024
@github-actions github-actions bot added changelog: fix Took care of something that wasn't working. type: bug The issue/PR is a confirmed bug. labels May 9, 2024
Comment on lines 33 to 34
typeof product.price_after_coupon_discount === 'number' &&
! isNaN( product.price_after_coupon_discount ) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these two? product.price_after_coupon_discount seems to be always a number

Copy link
Contributor Author

@ianlin ianlin May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there were not only purchase event would call the method getProductFieldObject(), for example begin_checkout event would call this method as well. Initially I wanted to test if product.price_after_coupon_discount is defined but since the price after discount could be 0 and 0 is treated as false, that's why I added these two conditions here.

Copy link
Contributor

@puntope puntope May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Thanks for clarifying. That might explain why we used

if ( typeof product.price_after_coupon_discount === 'number' )

But what about the NaN line check?

Also WDYT of doing something like:

if ( product?.price_after_coupon_discount < product.prices.price )

So we can save both checks number and nan

You can test it but in case product.price_after_coupon_discount is undefined this would be false still.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puntope

But what about the NaN line check?

I added it because I see typeof NaN is also a number, so added it for some prevention.

Also WDYT of doing something like:
if ( product?.price_after_coupon_discount < product.prices.price )
So we can save both checks number and nan
You can test it but in case product.price_after_coupon_discount is undefined this would be false still.

Ah good point, thanks for the info.

if (
typeof product.price_after_coupon_discount === 'number' &&
! isNaN( product.price_after_coupon_discount ) &&
product.price_after_coupon_discount !== product.prices.price
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about changing this to product.price_after_coupon_discount < product.prices.price I don't think we can have negative discounts.

product.price_after_coupon_discount !== product.prices.price
) {
data.discount =
product.prices.price - product.price_after_coupon_discount;
Copy link
Contributor

@puntope puntope May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to wrap this with formatPrice() as well. Otherwise, the discount is sent without decimals.

Screenshot 2024-05-10 at 16 25 13

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the nice catch!

Copy link
Contributor

@puntope puntope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @ianlin

The price is shown correctly but not the discount.

See my comments in the PR. Thanks

@ianlin
Copy link
Contributor Author

ianlin commented May 13, 2024

Hey @puntope, thanks for the review and nice suggestions. I've pushed more commits and it's ready for another round.

Copy link
Contributor

@puntope puntope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes @ianlin

I left one more comment regarding the need for the number and nan check. But approved in advance

As the test result will still be false if the discount is undefined.
@ianlin ianlin merged commit b026059 into trunk May 13, 2024
7 checks passed
@ianlin ianlin deleted the fix/417-use-discounted-price-when-available-in-purchase-event branch May 13, 2024 09:28
@puntope puntope mentioned this pull request May 14, 2024
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: fix Took care of something that wasn't working. type: bug The issue/PR is a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Item Revenue data sent to Google Analytics uses Product Price, not Order Item Total
2 participants