Shopify Functions apply discount based on Variant properties

Solved

Shopify Functions apply discount based on Variant properties

Sha_Hassan
Shopify Partner
12 0 4

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. 

 

Sha_Hassan_0-1695018577679.png

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. 

Accepted Solution (1)

JonasS
Shopify Partner
73 8 12

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.

Cobo-Sf : Your needs, our speed – Together, we succeed!
Feel free to let us know what you really want to change for your client.

View solution in original post

Replies 8 (8)

JonasS
Shopify Partner
73 8 12

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.

Cobo-Sf : Your needs, our speed – Together, we succeed!
Feel free to let us know what you really want to change for your client.
Sha_Hassan
Shopify Partner
12 0 4

I think you are right it only supports id and quantity. 

infinite_loop
Shopify Partner
4 0 0

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.

Sha_Hassan
Shopify Partner
12 0 4

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

Jay7777
Shopify Partner
3 0 1

Do shopify functions work for shopify draft orders?

gabrielvictoria
Shopify Partner
13 0 2

That is because the target to apply the discount is the product variant ID... and it should be line item id

infinite_loop
Shopify Partner
4 0 0

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?

gabrielvictoria
Shopify Partner
13 0 2

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