Hello,
Is it possible to apply a coupon to a cart line product variant that is only valid if there’s a cart line attribute “Magic Discount”?
Maybe there’s a better way to achieve what I want:
I have an app with an App theme extension that allows users to set their own price. the extension creates a form where users submit their price, upon submission I want to adjust the price of the product variant to the user set price. Since price cannot be updated during checkout process, I thought I would generate a discount and apply it to the cart line.
I need to ensure these coupon codes generated to adjust price to the user price are only used in the context of my app.
I would appreciate any guidance to approach the problem the right way.
Thank you!
This is as far as I got, I created a discount function, on the run.graphql I added this:
query RunInput {
cart {
lines {
id
merchandise {
... on ProductVariant {
id
title
}
}
attribute (key: "MagicDiscount"){
key
value
}
cost {
totalAmount {
amount
currencyCode
}
}
quantity
}
}
}
On the run.js I have the control to see if
function hasValidMagicDiscount(line) {
return (
line.attribute &&
line.attribute.key === "MagicDiscount" &&
line.attribute.value !== undefined &&
line.attribute.value !== null &&
line.merchandise &&
line.merchandise.__typename === "ProductVariant" &&
line.merchandise.id !== undefined &&
line.merchandise.id !== null
);
}
// The configured entrypoint for the 'purchase.product-discount.run' extension target
/**
* {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
const discounts = input.cart.lines
// Filter lines that have the hasValidMagicDiscount attribute
.filter(line => hasValidMagicDiscount(line))
.map(line => {
const discountAmount = 10;
return {
targets: [
{
productVariant: {
id: line.merchandise.id
}
}
],
value: {
fixedAmount: {
amount: discountAmount,
currencyCode: line.cost.totalAmount.currencyCode
}
}
};
if (!discounts.length) {
// You can use STDERR for debug logs in your function
console.error("No cart lines qualify for the BargainSDK discount.");
return EMPTY_DISCOUNT;
}
// The /shopify_function package applies JSON.stringify() to your function result
// and writes it to STDOUT
return {
discounts: discounts,
discountApplicationStrategy: DiscountApplicationStrategy.First
};
};
Unfortunately I get this on the debug:
{
"cart": {
"lines": [
{
"id": "gid://shopify/CartLine/0",
"attribute": null,
"cost": {
"totalAmount": {
"amount": "749.95",
"currencyCode": "EUR"
}
},
"quantity": 1
}
]
}
}
I don’t understand where is the error since I know for a fact the attribute MagicDiscount is being passed to the cartline because the cart is created programmatically with these attributes:
const query = `
mutation {
cartCreate(
input: {
lines: [
{
merchandiseId: "gid://shopify/ProductVariant/${variantId}",
quantity: ${quantity},
attributes: [
{
key: "MagicDiscount",
value: "${discount}"
}
]
}
]
}
) {
cart {
id
checkoutUrl
lines(first: 5) {
edges {
node {
id
quantity
merchandise {
... on ProductVariant {
id
title
}
}
}
}
}
}
}
}
`;
Mistery solved, it was a typo on the attribute name.