Join us for an upcoming Shopify Partner webinar on February 27, 2024. Discover the latest Checkout Extensibility features, and deep dive on improvements to Shopify Functions and Web Pixels. Register now for either the 10am EST or 2pm EST sessions.

Product Discount Function Bug - Line Item Quantities

originmaster
Shopify Partner
38 2 15

When using a product discount function we are seeing unpredictable behaviour in relation to subtotal pricing of cart items when updating quantities within the cart.

Steps to reproduce:
1. Add relevant items to cart.
2. Update quantities of each item.
3. Check the subtotal of each item - sometimes it is correct and other times not.

Here is an example:

This screenshot shows an incorrect subtotal for the second line. This happened after I had both items in cart with just 1 quantity, then I updated the second item quantity to 3, then the first item to 6.
wrong-pricing.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Here is the cart input to the product discount function when this occurred. Interestingly the second line item (the problematic one) seems to be split into 2 separate line items (CartLine 1 and CartLine 2), even though they appear as one line in the cart, so the quantities show as 1 and 2, when they should be represented as a single line with a quantity of 3.

 

 

{
    "buyerIdentity": null,
    "lines": [
      {
        "id": "gid://shopify/CartLine/0",
        "cost": {
          "amountPerQuantity": {
            "amount": "39.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "234.0"
          },
          "totalAmount": {
            "amount": "234.0"
          }
        },
        "quantity": 6,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/32821778645124",
          "product": {
            "id": "gid://shopify/Product/4699130232964",
            "vendor": "Some vendor"
          }
        }
      },
      {
        "id": "gid://shopify/CartLine/1",
        "cost": {
          "amountPerQuantity": {
            "amount": "65.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "65.0"
          },
          "totalAmount": {
            "amount": "65.0"
          }
        },
        "quantity": 1,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/31929170034820",
          "product": {
            "id": "gid://shopify/Product/4499836141700",
            "vendor": "Some other vendor"
          }
        }
      },
      {
        "id": "gid://shopify/CartLine/2",
        "cost": {
          "amountPerQuantity": {
            "amount": "65.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "130.0"
          },
          "totalAmount": {
            "amount": "130.0"
          }
        },
        "quantity": 2,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/31929170034820",
          "product": {
            "id": "gid://shopify/Product/4499836141700",
            "vendor": "Some other vendor"
          }
        }
      }
    ]
  }

 

 


The output is fine and works as it should:

 

 

{
  "discounts": [
    {
      "targets": [
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/32821778645124",
            "quantity": 6
          }
        },
        {
          "productVariant": {
            "id": "gid://shopify/ProductVariant/31929170034820",
            "quantity": 3
          }
        }
      ],
      "value": {
        "percentage": {
          "value": 15.000000000000005
        }
      },
      "message": "2kg Stack!"
    }
  ],
  "discountApplicationStrategy": "ALL"
}

 

 

 

I then updated the quantity of the second line item to 4, then back to 3 and the problem disappeared:
correct-pricing 2024-01-11 at 11.34.40 AM.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And sure enough the cart input to the function shows the line items grouped more predictably:

{
    "buyerIdentity": null,
    "lines": [
      {
        "id": "gid://shopify/CartLine/0",
        "cost": {
          "amountPerQuantity": {
            "amount": "39.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "234.0"
          },
          "totalAmount": {
            "amount": "234.0"
          }
        },
        "quantity": 6,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/32821778645124",
          "product": {
            "id": "gid://shopify/Product/4699130232964",
            "vendor": "Some vendor"
          }
        }
      },
      {
        "id": "gid://shopify/CartLine/1",
        "cost": {
          "amountPerQuantity": {
            "amount": "65.0"
          },
          "compareAtAmountPerQuantity": null,
          "subtotalAmount": {
            "amount": "195.0"
          },
          "totalAmount": {
            "amount": "195.0"
          }
        },
        "quantity": 3,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/31929170034820",
          "product": {
            "id": "gid://shopify/Product/4499836141700",
            "vendor": "Some other vendor"
          }
        }
      }
    ]
  }

The function output is identical both times.

As far as I can see this bug only happens when the cart line items are being split for some reason on the Shopify server side.

Replies 0 (0)