Shopify / Shopify/discounts-reference-app
Fix types: Comparing string not numbers to discount highest subtotal line item
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:
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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