{"status":422,"message":"Cart Error","description":"Cannot apply selling plan to variant"}

With the code below, I am trying to add an item to the cart, but it is throwing the error as in the title of this post.

  let variantData = {
    'id': 31638158934062,
    'quantity': 1,
    'selling_plan': 684949550
  };
  
  var url = '/cart.js';
  fetch(url, {method: 'GET'})
    .then(res => res.json())
    .then(response => {
      console.log('Success:', JSON.stringify(response))
      const cart = response;
      // Add item to the cart:
      var cartToken = cart.token;
      var url = window.Shopify.routes.root + 'cart/add.js';
      fetch(url, {
        method: 'POST',
        headers:{
          'Content-Type': 'application/json',
          'Accept': 'application/json'
        },
        body: JSON.stringify(variantData)
        })
        .then(res => res.json())
        .then(response => {
          console.log('Success:', JSON.stringify(response))
        })
        .catch(error => console.error('Error:', error));
      })
    .catch(error => console.error('Error:', error)
  );

Could you please help fix the issue?

Hello Pramodraam,

Thank you for reaching out to the Shopify community.

This generally happens if the selling plan id passed while calling add.js is incorrect. Here are a few of the troubleshooting steps to check on the issue:

  1. Navigate to the products section in the Shopify admin panel and ensure you have a selling plan associated to the variant that you are adding to the cart.

  1. Please ensure you are passing the selling plan id instead of selling plan group id when adding the product to the cart. To get the selling plan id of any product you can call /product_handle.js from the storefront.

Do give it a try and let me know if this was helpful.

Sinchain,

Recurpay Subscriptions