Get product options with subscription options in the cart.

Hello! We are developing an app to automatically apply discounts for customers who are affiliates. I created this discount function following the documentation for product discount functions, and I also created a UX screen in the admin to manage this discount. So far, so good, but now we’ve encountered an issue where we have products with a subscription option that we created using the “Bold Subscription” app, and we can’t apply the affiliate discount to these products. What I need to do is check in the cart if this product has the subscription option and if the user selected that option, but looking at the documentation, there’s no option to query the cart using GraphQL. Has anyone else needed to do something similar?

Hi Rafael,

This may not be supported currently for live stores but have you looked into the Discounts Allocator Function API for this? It’s currently in developer preview though so not available for live stores today.

Hello Liam, thank you for your response. I managed to solve my problem in a different way. I realized that the option to combine discounts would solve the issue I was facing.

I was able to create the discounts while testing in a test store. However, I am now encountering an error when trying to create this coupon in my production store. I will add my issue here, but if necessary, I will open another thread.

Based on the documentation, I created the following code for discount creation:

const baseDiscount = {
    functionId,
    title,
    combinesWith,
    startsAt: new Date(startsAt),
    endsAt: endsAt && new Date(endsAt),
    metafields: [{
      namespace: "$app:product-discount",
      key: "function-configuration",
      type: "json",
      value: JSON.stringify({
        percentage: configuration.percentage,
        status: status,
      }),
    }]
  };

  const response = await admin.graphql(
    `#graphql
    mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) {
      discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) {
        userErrors {
          field
          message
        }
        automaticAppDiscount {
          discountId
        }
      }
    }`,
    {
      variables: {
        automaticAppDiscount: {
          ...baseDiscount,
        },
      },
    },
  );

Running this in my test store, I can create the discount, but in my production store, I get the following error:

{
    "errors": [
        {
            "message": "Internal error. Looks like something went wrong on our end.\nRequest ID: ': 'fc1996c2-e3c7-40e7-b29c-261e7952c8ec-1724853349 (include this in support requests).",
            "extensions": {
                "code": "INTERNAL_SERVER_ERROR",
                "requestId": "': 'fc1996c2-e3c7-40e7-b29c-261e7952c8ec-1724853349"
            }
        }
    ]
}

I thought it might be something related to my app, so to clarify, I installed the “Shopify GraphQL App,” as mentioned in the documentation, and ran the GraphQL query through it:

// Mutation
mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) {
  discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) {
    userErrors {
      field
      message
    }
    automaticAppDiscount {
      discountId
    }
  }
}

// variables:
{
  "automaticAppDiscount": {
    "functionId": "3036c9dc-9fb7-47f6-8a9f-d05e3fe7190b",
    "title": "AFFILIATE",
    "startsAt": "2022-06-22T00:00:00",
    "combinesWith": {
      "productDiscounts": false,
      "orderDiscounts": false,
      "shippingDiscounts": true
    },
    "metafields": [
      {
        "namespace": "$app:product-discount",
        "key": "function-configuration",
        "type": "json",
        "value": "{\n  \"percentage\": 20,\n  \"status\": \"ACTIVE\"\n}"
      }
    ]
  }
}

I still get the same error, and only in the production store.

Hi Rafael,

The issue you’re facing arises from the lack of direct support for querying the cart using GraphQL to check for subscription products added via the “Bold Subscription” app. To address this, a possible solution would involve inspecting the cart’s contents using the cart APIs provided by Shopify or Bold. You can check the cart’s line items to identify if any products are associated with a subscription by looking for relevant metadata or tags that indicate subscription status. Once you’ve identified the subscription items, you can implement custom logic to determine whether the affiliate discount should be applied based on whether the user selected the subscription option for those products.