How to access Compare At Price in Checkout Extensibility Discount App

I am working on converting a Script into a Discount Function before I upgrade to checkout extensibility.

I have created the discount app and it seems to be working fine, but the last piece is to prevent the automatic discount from being applied to product variants that have a compare at price higher than the price.

Using the old Shopify Scripts, we were able to access .compare_at_price, but I am not seeing anything similar in GraphQL ProductVariant object…

https://shopify.dev/docs/api/functions/reference/product-discounts/graphql/common-objects/productvariant

Is there any way for me to access the compare at price in the extension?

Thanks in advance!

-Victor

Hi Victor,

While discount functions can be used to apply discounts based on certain conditions, they don’t have access to the “compare at price” in the same way that scripts did previously. You’l need to explore alternative methods for applying discounts, possibly by using the available pricing data that is accessible through the new extensibility features.

Actually - after looking into this more, it’s possible the compareAtAmountPerQuantity field on the CartLineCost object might work for you?

1 Like

Awesome Liam, thanks for the help! I looked into it and that was exactly what I needed.

Here is my run.graphql query:

query RunInput {
  cart {
    lines {
      id
      quantity
      cost {
        amountPerQuantity {
          amount
        }
        compareAtAmountPerQuantity  {
          amount          
        }
      }
      merchandise {
        __typename
        ... on ProductVariant {
          id
        }
      }
      discount: attribute(key: "_discount") {
        value
      }
      builderId: attribute(key: "builder_id") {
        value
      }
    }
  }
}

And here is where I filter cartlines based on a discount property and whether the compare at price is equal to price:

export function run(input) {
  var discountPercentage = {};
  var cartLinePercentage = {};
  const targets = input.cart.lines
    // Only include cart lines with a _discount property and compare at price equal to price if it exists
    .filter(line => line.discount &&  
      line.merchandise.__typename == "ProductVariant" &&
      line.cost.compareAtAmountPerQuantity ? (line.cost.compareAtAmountPerQuantity?.amount == line.cost.amountPerQuantity?.amount) : true)
    .map(line => {
      const cartLine = /**   {CartLine} */ (line);
      discountPercentage[line.builderId?.value] = DISCOUNTS[line.discount?.value] || 0;
      cartLinePercentage[line.id] = DISCOUNTS[line.discount?.value] || 0;
      return /**  {Target} */ ({
        // Use the cart line ID to create a discount target
        cartLine: {
          id: cartLine.id,
        }
      });
    });

Appears to be working great!