All things Shopify and commerce
Hi Shopify Folks! I am facing an issue in applying discount based on variant properties through Shopify functions.
So what I wanted is that e.g if I have a variant A in the cart with property deal, the shopify discount should applied on this item. But if then I add the same variant A without the property deal the discount should not apply.
Right now I have written the logic based on property but it apply discount on the variant irrespective of whether it has that property or not.
So my understanding is that the shopify functions apply discount based on variant id only.
Is there a way I can achieve this?
Here is my shopify function logic
let items = input.cart.lines.filter((line) => line.att3?.value != null && line.quantity == 1 );
let firstItemDealId = items[0].att3.value;
if(firstItemDealId !== null ) {
targets = items
// Only include cart lines with a quantity of two or more
// and a targetable product variant
.filter(line => line.att3.value == firstItemDealId &&
line.merchandise.__typename == "ProductVariant")
.map(line => {
discount = line.att2.value.replace('%', '');
totalProducts = line.att1.value;
const variant = /** @type {ProductVariant} */ (line.merchandise);
//discount = line.properties.discount;
//console.log(discount);
return /** @type {Target} */ ({
// Use the variant ID to create a discount target
productVariant: {
id: variant.id
}
});
});
}
where line.att3.value is that property based on which I need to apply discount.
Here you can see the discount applied on first 2 line items. Variant is same but property is different.
Any help would be appreciated, Thanks.
Solved! Go to the solution
This is an accepted solution.
Actually, I think Shopify Functions now does not support applying discount by properties. The targets only support ID and quantity.
This is an accepted solution.
Actually, I think Shopify Functions now does not support applying discount by properties. The targets only support ID and quantity.
I think you are right it only supports id and quantity.
Are there currently any workaround for this? I mean we could add a logic where we could check if an item has a custom property and count how many quantities to apply the discount for. But what if there are two identical items. One with a custom property and one without , even though we would be targeting using the variantID, we could potentially discount the one that does not have the custom property.
No Shopify functions apply discount based on the variant id so no other logic will work at the moment. We will have to wait for any future update I guess if they provide such option
Do shopify functions work for shopify draft orders?
That is because the target to apply the discount is the product variant ID... and it should be line item id
I don't think you could target it with the line item ID based on the input object:
https://shopify.dev/docs/api/functions/reference/order-discounts/graphql/common-objects/target
Have you tried using the target ID and have worked for you?
I don't express myself correctly... Shopify doesn't allow you to target line item id, only variant id... that is the problem I guess
Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024The Hydrogen Visual Editor is now available to merchants in Shopify Editions | Summer '...
By JasonH Sep 2, 2024Note: Customizing your CSS requires some familiarity with CSS and HTML. Before you cust...
By JasonH Aug 12, 2024