Issues in Assigning Selling Plan to Cart Line Item

I have already created a selling plan using GraphQl API. Now from the frontend (liquid template), I am giving an ajax call using Shopify Ajax API (Cart) (https://shopify.dev/api/ajax/reference/cart#update-selling-plans)/). Below is my code in the liquid template:

if ({{ cart.item_count }} > 0) {
  setTimeout(() => {
    let formData = {
      'line': {{ index }},
      'selling_plan': 602308833
    };

    fetch(window.Shopify.routes.root + 'cart/change.js', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(formData)
      })
      .then(response => {
        return response.json();
      })
      .catch((error) => {
        console.error('Error:', error);
      });
  }, {{ index }}* 500);
}

Where {index} will be changed as with the looping over the cart line items. For each Cart Line Item, this request is sent.

When I send a request using the above code (please note that the “id” key is commented out and the “line” key is used. I receive the below response:

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

I am using the “line” property because it is mentioned in the documentation (https://shopify.dev/api/ajax/reference/cart#update-selling-plans))

But when I comment out “line” property and uncomment “id”. The below response is received:

{
  "token": "c4ccfe3bc6116428e2710e753837d852",
  "note": null,
  "attributes": {},
  "original_total_price": 100000,
  "total_price": 100000,
  "total_discount": 0,
  "total_weight": 0,
  "item_count": 2,
  "items": [
    {
      "id": 42980749148385,
      "properties": {
        "_main_bundle": "bundle product production 08 jul 1"
      },
      "quantity": 1,
      "variant_id": 42980749148385,
      "key": "42980749148385:3b37d914359101f34d07b3210c26a5d5",
      "title": "Robe 3 - XS/S / Solid White",
      "price": 50000,
      "original_price": 50000,
      "discounted_price": 50000,
      "line_price": 50000,
      "original_line_price": 50000,
      "total_discount": 0,
      "discounts": [],
      "sku": null,
      "grams": 0,
      "vendor": "qfn-store",
      "taxable": true,
      "product_id": 7737457344737,
      "product_has_only_default_variant": false,
      "gift_card": false,
      "final_price": 50000,
      "final_line_price": 50000,
      "url": "/products/robe-3?variant=42980749148385",
      "featured_image": {
        "aspect_ratio": 1,
        "alt": "Robe 3",
        "height": 768,
        "url": "https://cdn.shopify.com/s/files/1/0628/1258/6209/products/robe_white.jpg?v=1656902684",
        "width": 768
      },
      "image": "https://cdn.shopify.com/s/files/1/0628/1258/6209/products/robe_white.jpg?v=1656902684",
      "handle": "robe-3",
      "requires_shipping": true,
      "product_type": "",
      "product_title": "Robe 3",
      "product_description": "What a robe. Wear it and the do Party like a RockStart",
      "variant_title": "XS/S / Solid White",
      "variant_options": [
        "XS/S",
        "Solid White"
      ],
      "options_with_values": [
        {
          "name": "Size",
          "value": "XS/S"
        },
        {
          "name": "Color",
          "value": "Solid White"
        }
      ],
      "line_level_discount_allocations": [],
      "line_level_total_discount": 0
    },
    {
      "id": 42968690491617,
      "properties": {
        "_main_bundle": "bundle product production 08 jul 1"
      },
      "quantity": 1,
      "variant_id": 42968690491617,
      "key": "42968690491617:523554e19306dac60139404423c844d8",
      "title": "Bed Sheet 3 - Twin / Essentials Solid White",
      "price": 50000,
      "original_price": 50000,
      "discounted_price": 50000,
      "line_price": 50000,
      "original_line_price": 50000,
      "total_discount": 0,
      "discounts": [],
      "sku": null,
      "grams": 0,
      "vendor": "qfn-store",
      "taxable": true,
      "product_id": 7732197064929,
      "product_has_only_default_variant": false,
      "gift_card": false,
      "final_price": 50000,
      "final_line_price": 50000,
      "url": "/products/bed-sheet-3?variant=42968690491617",
      "featured_image": {
        "aspect_ratio": 1,
        "alt": "Bed Sheet 3",
        "height": 768,
        "url": "https://cdn.shopify.com/s/files/1/0628/1258/6209/products/bed_sheet_solid_white_f4a7809a-69ed-41c2-9bc2-356b6ddeae82.jpg?v=1656905521",
        "width": 768
      },
      "image": "https://cdn.shopify.com/s/files/1/0628/1258/6209/products/bed_sheet_solid_white_f4a7809a-69ed-41c2-9bc2-356b6ddeae82.jpg?v=1656905521",
      "handle": "bed-sheet-3",
      "requires_shipping": true,
      "product_type": "",
      "product_title": "Bed Sheet 3",
      "product_description": "An ideal bedsheet for flawless sleep. Keep sleeping and die sleeping ;)",
      "variant_title": "Twin / Essentials Solid White",
      "variant_options": [
        "Twin",
        "Essentials Solid White"
      ],
      "options_with_values": [
        {
          "name": "Size",
          "value": "Twin"
        },
        {
          "name": "Color",
          "value": "Essentials Solid White"
        }
      ],
      "line_level_discount_allocations": [],
      "line_level_total_discount": 0
    }
  ],
  "requires_shipping": true,
  "currency": "AED",
  "items_subtotal_price": 100000,
  "cart_level_discount_applications": []
}

No error is thrown. Everything seems to work fine. But on the checkout page prices are not adjusted. Below is the extract from the mutation that was used to create the selling plan.

pricingPolicies: [{
    fixed: {
        adjustmentType: PERCENTAGE
        adjustmentValue: {
            percentage: 15.0
        }
    }
}]

Any help will be regarded.

Thanks

@JoshArnold

2 Likes