Happening now | Office Hours: Customizing Your Theme With Moeed | Ask your questions now!
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Help structuring return to apply custom discount to each cart line - DiscountApplicationStrategy

Help structuring return to apply custom discount to each cart line - DiscountApplicationStrategy.All

DeeDee-TDY
Shopify Partner
51 4 7

I am having trouble structuring the standard output required to apply a different product discount to each cart item variant ID. I am using app and extension api-version 2024-01, app and cli versions 3.53.0. My product-discount function standard input looks like the below (and I would like the standard output to similarly reflect each variant ID's separate discount value). STDIN:

{
  "cart": {
    "lines": [
      {
        "quantity": 1,
        "attribute": {
          "key": "item-discount",
          "value": "20.00"
        },
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/45970165236027"
        }
      },
      {
        "quantity": 1,
        "attribute": {
          "key": "item-discount",
          "value": "10.00"
        },
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/45970164515131"
        }
      }
    ]
  }
}

But my standard output only applies the first selected product discount (10%) and looks like this - STDOUT:

{
  "discounts": [
    {
      "targets": [
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/45970165236027"
          }
        },
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/45970164515131"
          }
        }
      ],
      "value": {
        "percentage": {
          "value": "10.00"
        }
      }
    }
  ],
  "discountApplicationStrategy": "ALL"
}

 and I would like the output to look something like this instead:

{
  "discounts": [
    {
      "targets": [
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/45970164515131"
          }
        }
      ],
      "value": {
        "percentage": {
          "value": "10.00"
        }
      }
    }
    {
      "targets": [
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/45970165236027"
          }
        }
      ],
      "value": {
        "percentage": {
          "value": "20.00"
        }
      }
    }
  ],
  "discountApplicationStrategy": "ALL"
}

My return code in the js is as follows, can anyone help direct me on how to modify the code to output the associated discount for each variant id (as shown in the standard input at the top)? Since DiscountApplicationStrategy.All is fairly new, there's not a lot of examples I have found on how I could accomplish associating a custom discount for each variant id.

 

 

 

  return {
    discounts: [
      {
        targets, 	// Apply the discount to the collected targets
        value: {
          percentage: { 	// Define a percentage-based discount (default=0.00)
            value: discount	// Use variable value of discount per variant ID
          }
        }
      }
    ],
    discountApplicationStrategy: DiscountApplicationStrategy.All
  };

 

 

 

 

Replies 3 (3)

DeeDee-TDY
Shopify Partner
51 4 7

I should probably add the code showing the discount mapped to each line:

      .filter(line => line.quantity >= configuration.quantity &&
        line.merchandise.__typename == "ProductVariant" &&
        line.attribute?.value != "0.00")
      .map(line => {
        discount = line.attribute.value.toString(); //override the default discount for each line.attribute.value
        const variant = /** @type {ProductVariant} */ (line.merchandise);
        console.error("DEBUG: Variant ID: "+variant.id+", Variant Discount: "+line.attribute?.value);
        return /** @type {Target} */ ({
          productVariant: { 	// Use the product variant ID to create a discount target
            id: variant.id
          }
        });
      });
Unleashedmyself
Shopify Partner
2 0 0

hi @DeeDee-TDY ! do you know how to apply discounts only for target line_item, not for all line_items with the same id's.
I have this issue, I need to apply discount for line_item with property "is_free", but for 'targets' we use product variant ID to create a discount target..

 

 

query RunInput {
  cart {
    lines {
      quantity
      custom_data: attribute(key: "is_free") {
         value
      }
      merchandise {
        __typename
        ...on ProductVariant {
            id
        }
      }
    }
  }
}



const targets = input.cart.lines
    .filter(line => line.merchandise.__typename == "ProductVariant" && line.custom_data && line.custom_data.value == "true")
    .map(line => {
      const variant = /** @type {ProductVariant} */ (line.merchandise);
      return /** @type {Target} */ ({
        productVariant: {
          id: variant.id,
        }
      });
    });

 

 

 H17SQ4n.png

DeeDee-TDY
Shopify Partner
51 4 7

Sorry I do not know how to apply to the line regardless of variantID. I believe at the time of the original post, it was not possible, but perhaps you can ask a Shopify team member if this feature is now available.