Assignign Subscription Plans to Variants

Building a new app. I’ve got a button that does the following:

  1. Creates a new product

  2. Creates a variant on new product

  3. Creates a new selling plan group.

  4. Assigns the subscription selling plan group to the variant.

All of these things seem to happen successfully. In the Dawn theme, I’ve added a variant selector. When I pick the variant with the recurring subscription plan assigned to it and checkout, there is no indication that the subscription has successfully started. As far as I can tell, its a one-time purchase.

My question is this:

1) how can I tell if the subscription has been successfully started (aside from waiting 24hrs to see if its renewed)?

2) Beyond simply submitting the variant with the subscription assigned to it, is there more I need to submit to checkout to initiate a successful subscription?

export async function action({ request }) {
    const { admin } = await authenticate.admin(request);

    const productResponse = await admin.graphql(`mutation {
        productCreate(input: {
        title: "Sweet new productzzY",
        productType: "TEST Product",
        vendor: "JadedPixel",
        options: ["Interval"]
        }) {
        product {
            id
            options {
            id
            name
            }
        }
        }
    }`,
    );

    // Convert the response into JSON
    const productData = await productResponse.json();
    const productId = productData.data.productCreate.product.id;

    const variantResponse = await admin.graphql(`
    mutation {
        productVariantCreate(input: {
            productId: "`+productId+`",
            price: "39.99",
            options: ["1 Week"],
            sku: "UNIQUE-SKU",
            title: "New Variant Title"
        }) {
          productVariant {
            id
          }
          userErrors {
            field
            message
          }
        }
      }
    `);

    const variantData = await variantResponse.json();
    const variantId =  variantData.data.productVariantCreate.productVariant.id;
    
    const planResponse = await admin.graphql(`
    mutation {
    sellingPlanGroupCreate(input: {
        name: "Subscribe and save",
        merchantCode: "subscribe-and-save",
        options: ["Delivery every"],
        position: 1,
        sellingPlansToCreate: [
        {
            name: "Delivered every week",
            options: "1 Day",
            position: 1,
            category: SUBSCRIPTION,
            billingPolicy: {
            recurring: {
                interval: DAY,
                intervalCount: 1,
                anchors: { type: WEEKDAY, day: 1 }
            }
            },
            deliveryPolicy: {
            recurring: {
                interval: DAY,
                intervalCount: 1,
                anchors: { type: WEEKDAY, day: 1 },
                preAnchorBehavior: ASAP,
                cutoff: 0,
                intent: FULFILLMENT_BEGIN
            }
            },
            pricingPolicies: [
            {
                fixed: {
                adjustmentType: PERCENTAGE,
                adjustmentValue: { percentage: 15.0 }
                }
            }
            ]
        }
        ]
    }) {
        sellingPlanGroup {
        id
        }
        userErrors {
        field
        message
        }
    }
    }
    `);

    const planData = await planResponse.json();
    const planId =  planData.data.sellingPlanGroupCreate.sellingPlanGroup.id;
    // Logging the JSON to see the structure

    const planAssocResponse = await admin.graphql(`
    mutation {
        sellingPlanGroupAddProductVariants(id: "`+planId+`", productVariantIds: ["`+variantId+`"]) {
        userErrors {
            field
            message
        }
        }
    }
    `);

Hey @finedining1

You’ll need to pass along the selling plan ID when the customer adds the variant to cart as a subscription. More info here.

Thanks - I’ve got the form setup to pass along the selling Plan ID but I can’t seem to access it from the frontend. While the selling plans appear in the product screen on the admin side (see below), they don’t appear in the product object. I think there is an issue with how I am assigning the variants to the selling plan group.

Hey @finedining1

I don’t believe that information is available via the AJAX API. Try using this example code in your theme.

Let me know if you get stuck.

To be clear, I got that console.log of the product object by adding this to the product page:


As for the demo code, I’ve tried several different ways over the last few days with no luck. I’ve been adding itt to the default dawn theme product page like so:

Okay its been a week stuck on this issue. I’ve completely refactored my app. I’ve gone through the subscription documentation, refactored my code, checked for correct scopes and the problem persists. I can successfully create subscriptions on the merchant side but they are not accessible on the storefront. I’m using the example code provided in the documentation on a vanilla dawn theme. No errors in the selling plan creation or association with the productid. Everything works except for the storefront. I’ve printed the product object into the console and there is no trace of selling plan information. Really not sure what is left to check at this point. Any ideas would be super appreciated.

Okay. Figured it out. I needed to add a checkout-system besides ‘bogus payment processor’ to get the store to make the store eligible for subscription. Duh.

1 Like