How can I exclude subscription items from discount function?

How can I exclude subscription items from discount function?

Ryoshin
Shopify Partner
40 3 9

I've been having quite a few issues with this type of function. The most pressing one right now is that it doesn't seem like I can decide which line items to display the discounts on.

None of my discounts should apply to subscriptions, and I can exclude them from the quantity. The problem is I can't select specifically the variants with no subscription plan in the cart because they all have the same id. So if I have the same product with and without sub it will randomly decide which products to apply it on. How can I effectively exclude subscription lines from the discount?

For example:

4 of Product X with no subs = 25% off
3 of Product X with 30 day sub = no discount
1 of Product X with 14 day sub = no discount

What happens right now is something like:

2 of Product X with no subs = 25% off
2 of Product X with no subs = no discount
1 of Product X with 30 day sub = 25% off
2 of Product X with 30 day sub = no discount
1 of Product X with 14 day sub = 25% off


Replies 3 (3)

Liam
Community Manager
3108 344 911

Hi Ryoshin,

 

The issue you're encountering is a common one in where a single product has multiple variants with different subscription plans or no subscription at all. You want to apply a discount to only one variant of this product, but currently, the discount is applied randomly to any variant of the product.

Unfortunately, Shopify's discount management system doesn't provide an out-of-the-box solution for specifying which variant of a product should receive the discount. This is because Shopify considers the product as a whole when applying discounts, not the specific variants.

 

One possible workaround could be to use the Admin API and scripting capabilities to create a more sophisticated discount logic, eg:

  1. Identify the Subscription Variants: Using the ProductVariant object with the sellingPlanGroupCount field, you can identify the product variants that are associated with a subscription plan. This will let you know which variants should not receive the discount.

  2. Create Custom Logic for Discounts: After identifying the subscription variants, you can create a custom script to apply discounts only to the non-subscription variants of a product. This script can run whenever a customer adds a product to their cart.

  3. Apply the Custom Discount Logic: the script to apply your custom discount logic. This script can be triggered when a product is added to the cart or when the customer checks out.

Would an approach like this work for your use case?

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

Ryoshin
Shopify Partner
40 3 9

I'm not sure what you mean by scripts. Do you mean the function script or shopify scripts which are being deprecated? If it's shopify scripts I did install it on a dev and live stores before it being removed from the store, but would it be a good approach given it's being deprecated?

Right now I can identify which variants are being purchased with and without sub, and I can adjust quantity adequately, but it displays randomly to the customer. So if 3 should be discounted it may show 2 no subs and 1 sub as discounted when all 3 should be no subs.

yannlombard
Shopify Partner
2 0 1

Same problem here two years later.

 

Based on product-discount result functions, something like this could be great for order-discount:

{
  "discountApplicationStrategy": "MAXIMUM",
  "discounts": [
    {
      "message": "10% off",
      "targets": [
        {
          "cartLine": {
            "id": "gid://shopify/CartLine/2",
            "quantity": 2
          }
        }
      ],
      "value": {
        "percentage": {
          "value": "10"
        }
      }
    }
  ]
}

 

Or:

{
  "discountApplicationStrategy": "MAXIMUM",
  "discounts": [
    {
      "message": "10% off",
      "targets": [
        {
          "orderSubtotal": {
            "excludedCartLineIds": ["gid://shopify/CartLine/2"]
          }
        }
      ],
      "value": {
        "percentage": {
          "value": "10"
        }
      }
    }
  ]
}

 

Yann Lombard
Maker of Yaco Progress Bar