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
};
