Shopify / function-examples

MIT License
182 stars 52 forks source link

Allow Merge Bundles to share Cart Lines that have the same Product Variant #472

Closed patricklinpl closed 6 months ago

patricklinpl commented 6 months ago

Before this fix:

Bundles that share the same components would not be able to merge if the product variant had the correct line quantity but was identified on the same line.

Example: Given the following Bundle definition:

Parent: ProductVariant/101112, quantity 1

Parent: ProductVariant/192021, quantity 1

input:

{
        "id": "gid://shopify/CartLine/7",
        "quantity": 2,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/131415",
          "title": "Shared Bundle Component",
          "component_parents": {
            "value": "[{\"id\":\"gid://shopify/ProductVariant/101112\",\"component_reference\":{\"value\":[\"gid://shopify/ProductVariant/131415\",\"gid://shopify/ProductVariant/161718\"]},\"component_quantities\":{\"value\":[1,1]}}, {\"id\":\"gid://shopify/ProductVariant/192021\",\"component_reference\":{\"value\":[\"gid://shopify/ProductVariant/131415\",\"gid://shopify/ProductVariant/222324\"]},\"component_quantities\":{\"value\":[1,1]}}]"
          }
        }
      },
      {
        "id": "gid://shopify/CartLine/8",
        "quantity": 1,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/161718",
          "title": "Bundle Component",
          "component_parents": {
            "value": "[{\"id\":\"gid://shopify/ProductVariant/101112\",\"component_reference\":{\"value\":[\"gid://shopify/ProductVariant/131415\",\"gid://shopify/ProductVariant/161718\"]},\"component_quantities\":{\"value\":[1,1]}}]"
          }
        }
      },
      {
        "id": "gid://shopify/CartLine/9",
        "quantity": 1,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/222324",
          "title": "Bundle Component",
          "component_parents": {
            "value": "[{\"id\":\"gid://shopify/ProductVariant/192021\",\"component_reference\":{\"value\":[\"gid://shopify/ProductVariant/131415\",\"gid://shopify/ProductVariant/222324\"]},\"component_quantities\":{\"value\":[1,1]}}]"
          }
        }
      }

[Expected] we would expect two merge bundles when given quantity 2 of "id": "gid://shopify/CartLine/7"

[Before] - Before the fix, the function would take the entire quantity of "id": "gid://shopify/CartLine/7" and not merge the remaining quantity

{
      "merge": {
        "cartLines": [
          {
            "cartLineId": "gid://shopify/CartLine/7",
            "quantity": 1
          },
          {
            "cartLineId": "gid://shopify/CartLine/8",
            "quantity": 1
          }
        ],
        "parentVariantId": "gid://shopify/ProductVariant/101112"
      }
  }

[After] - After the fix, the function will take into account the remaining quantity and be able to resolve remaining bundles if cart lines exist

{
      "merge": {
        "cartLines": [
          {
            "cartLineId": "gid://shopify/CartLine/7",
            "quantity": 1
          },
          {
            "cartLineId": "gid://shopify/CartLine/8",
            "quantity": 1
          }
        ],
        "parentVariantId": "gid://shopify/ProductVariant/101112"
      }
    },
    {
      "merge": {
        "cartLines": [
          {
            "cartLineId": "gid://shopify/CartLine/7",
            "quantity": 1
          },
          {
            "cartLineId": "gid://shopify/CartLine/9",
            "quantity": 1
          }
        ],
        "parentVariantId": "gid://shopify/ProductVariant/192021"
      }
    }