For discussing the development and usage of Checkout UI extensions, post-purchase extensions, web pixels, Customer Accounts UI extensions, and POS UI extensions
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...
Is there any way for me to access the compare at price in the extension?
Thanks in advance!
-Victor
Solved! Go to the solution
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
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
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
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!