Shopify / Shopify/discounts-reference-app

Fix types: Comparing string not numbers to discount highest subtotal line item

Open
#41 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
10
Forks
5
PR merge metrics
No merged PRs in 30d

Description

User reported example does work as written/pulled into the docs, and provided a suggestion for how to fix.

See https://github.com/Shopify/shopify-dev/issues/61273 for original issue.

The example template is wrong as the end goal is to discount the highest subtotal lineitem. The example is comparing the cost of these but as strings, not numbers. It should actually be
const maxCartLine = input.cart.lines.reduce((maxLine, line) => {
if (Number(line.cost.subtotalAmount.amount) > Number(maxLine.cost.subtotalAmount.amount)) {
return line;
}
return maxLine;
}, input.cart.lines[0]);

How I interpret:

https://github.com/Shopify/discounts-reference-app/blob/09c22fb25d3a3e30ea2e68137ebfa72066823ccf/examples/javascript/default/extensions/discount-function/src/cart_lines_discounts_generate_run.js#L24-L29

User's suggestion:

  const maxCartLine = input.cart.lines.reduce((maxLine, line) => {
    if (Number(line.cost.subtotalAmount.amount) > Number(maxLine.cost.subtotalAmount.amount)) {
      return line;
    }
    return maxLine;
  }, input.cart.lines[0]);

to compare as numbers rather than strings.

Closing the original docs issue in favor of this one.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open examples/javascript/default/extensions/discount-function/src/cart_lines_discounts_generate_run.js at the lines identified in the issue and inspect how the highest-subtotal cart line is selected. Verify that the example compares subtotal amounts numerically and that the resulting line is the one with the highest subtotal.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.