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

isDiscounted compares 2 strings instead of 2 numbers #2354

Open
wants to merge 1 commit into
base: v1.x-2022-07
Choose a base branch
from

Commits on Nov 27, 2022

  1. isDiscounted compares 2 strings instead of 2 numbers

    While working with the demo-store template, I was having issues getting the discounts to show up.
    
    My product:
    - price: 50.0
    - compareAtPrice: 100.0
    
    This is an item that I was expecting to show a sale price. I loaded the demo-store and was unable to see the sale. I debugged the issue to the utils isDiscounted() function.
    
    ```
    export function isDiscounted(price: MoneyV2, compareAtPrice: MoneyV2) {
      if (compareAtPrice?.amount > price?.amount) {
        return true;
      }
      return false;
    }
    ```
    
    We see that we are comparing a MoneyV2.amount with another MoneyV2.amount. What's wrong with this?
    
    ```
    export declare type MoneyV2 = {
        __typename?: 'MoneyV2';
        /** Decimal money amount. */
        amount: Scalars['Decimal'];
        /** Currency of the money. */
        currencyCode: CurrencyCode;
    };
    ```
    
    In the type definition, we see that amount: Scalars['Decimal']. However, when I access the attribute, I actually am getting back a string representation of a decimal.
    
    So for my example
    ```
    compareAtPrice?.amount > price?.amount
    // "100.0" > "50.0"
    // => false
    ```
    
    Instead, let's cast the strings to their decimals and compare.
    superjova committed Nov 27, 2022
    Configuration menu
    Copy the full SHA
    4cda872 View commit details
    Browse the repository at this point in the history