Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

How to access Compare At Price in Checkout Extensibility Discount App

Solved

How to access Compare At Price in Checkout Extensibility Discount App

vbjornsson
Shopify Partner
9 1 2

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...

vbjornsson_0-1721950078231.png

vbjornsson_1-1721950154406.png

 

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

 

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

 

Thanks in advance!

-Victor

 

Accepted Solution (1)

Liam
Community Manager
3108 344 911

This is an accepted solution.

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

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 3 (3)

Liam
Community Manager
3108 344 911

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.

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Liam
Community Manager
3108 344 911

This is an accepted solution.

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

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

vbjornsson
Shopify Partner
9 1 2

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 = /**  @type {CartLine} */ (line);
      discountPercentage[line.builderId?.value] = DISCOUNTS[line.discount?.value] || 0;
      cartLinePercentage[line.id] = DISCOUNTS[line.discount?.value] || 0;
      return /** @type {Target} */ ({
        // Use the cart line ID to create a discount target
        cartLine: {
          id: cartLine.id,
        }
      });
    });

 

 

Appears to be working great! 

 

vbjornsson_1-1722446015940.png

 

 

vbjornsson_0-1722445962637.png