Skip to content

Shopify Function Cart Transform Line merge is not working on API version 2025-01 or 2025-07 #59

@pierrewebdev

Description

@pierrewebdev

I was testing my Shopify App recently and noticed that the Cart Transform merge is no longer working on version 2025-01.

In the 2025-01 version of the API says that you use are supposed to use the term merge in the Shopify Function but the most up to date and stable version of the api (2025-07) it says you are supposed to use linesMerge.

Below I put a code block of the following:

  • My GraphQL input data
  • The contents of my run.js file
  • The output as Shown in my dev dashboard

If you take a look at my run.js code, you will see i am using the merge command at the end of the run function.

My code was working just a couple of weeks ago so I’m not sure what is going on. Please let me know how I can fix this.

//========== GraphQL Input Data for the Shopify Function

{
  "cart": {
    "lines": [
      {
        "id": "gid:\/\/shopify\/CartLine\/98a6b1a8-f7ec-401b-8287-b124c47c43fb",
        "quantity": 1,
        "bundleId": {
          "value": "5"
        }
      },
      {
        "id": "gid:\/\/shopify\/CartLine\/c6456931-21c8-4f66-8704-eed9314783fa",
        "quantity": 1,
        "bundleId": {
          "value": "5"
        }
      },
      {
        "id": "gid:\/\/shopify\/CartLine\/94714ab5-e458-4fe3-8d04-1c8ee44af690",
        "quantity": 1,
        "bundleId": {
          "value": "5"
        }
      },
      {
        "id": "gid:\/\/shopify\/CartLine\/b04dc235-3904-4643-8640-1a58ae37d680",
        "quantity": 1,
        "bundleId": {
          "value": "5"
        }
      }
    ]
  },
  "shop": {
    "metafield": {
      "value": "{\"parentVariantId\":\"gid:\/\/shopify\/ProductVariant\/51198624563487\",\"bundlesArray\":[{\"name\":\"Test Bundle\",\"id\":5,\"bundle_display_name\":\"\",\"bundle_checkout_title\":\"\",\"status\":\"active\",\"discount_type\":\"percentage\",\"static_discount_value\":\"20\",\"tiered_discounts\":\"[]\"}]}"
    }
  }
}

//========== Run.js file
export function run(input) {
  // Stores cart lines grouped by their bundle ID property
  const groupedItems = {};
  const metafieldData = JSON.parse(input.shop.metafield.value)

  const parentVariantId = metafieldData.parentVariantId
  const bundlesArray = metafieldData.bundlesArray
  const cartLines = input.cart.lines


  cartLines.forEach(line => {
    const bundleId = line.bundleId
    if(!bundleId?.value) return

    //create a new grouping for the bundle id if it doesn't have one yet
    if(!groupedItems[bundleId.value]){
      groupedItems[bundleId.value] = []
    }

    groupedItems[bundleId.value].push(line)
  })

  console.log(JSON.stringify(groupedItems))

  const cartOperations = Object.values(groupedItems).map(group => {
    const totalItemsInBundle = group.reduce((sum, line) => sum + line.quantity, 0);
    const bundleId = group[0].bundleId.value;

    const discountPercentage = calculatePercentageDiscount(bundlesArray, bundleId, totalItemsInBundle);
    const updatedBundleTitle = overrideBundleTitle(bundlesArray, bundleId);

    const lineItems = group.map(line => {
      return {
        cartLineId: line.id,
        quantity: line.quantity
      }
    })


    return{
      merge: {
        cartLines: lineItems,
        parentVariantId: parentVariantId,
        price: {
          percentageDecrease: {
            value: discountPercentage
          }
        },
        //Conditional object spread
        ...(updatedBundleTitle && { title: updatedBundleTitle })
      }
    }
  })

  return {
    operations: cartOperations
  }
};

//========= Result of the Shopify Function:
{
  "operations": [
    {
      "linesMerge": {
        "cartLines": [
          {
            "cartLineId": "gid:\/\/shopify\/CartLine\/98a6b1a8-f7ec-401b-8287-b124c47c43fb",
            "quantity": 1
          },
          {
            "cartLineId": "gid:\/\/shopify\/CartLine\/c6456931-21c8-4f66-8704-eed9314783fa",
            "quantity": 1
          },
          {
            "cartLineId": "gid:\/\/shopify\/CartLine\/94714ab5-e458-4fe3-8d04-1c8ee44af690",
            "quantity": 1
          },
          {
            "cartLineId": "gid:\/\/shopify\/CartLine\/b04dc235-3904-4643-8640-1a58ae37d680",
            "quantity": 1
          }
        ],
        "parentVariantId": "gid:\/\/shopify\/ProductVariant\/51198624563487",
        "price": {
          "percentageDecrease": {
            "value": 20
          }
        }
      }
    }
  ]
}

You'll notice in the code that I have some helper functions to calculate a discount percentage and the title of the bundle for the final return of the merge operation. I have confirmed that those functions are working just fine and are not causing any issues.

I have also tried updating my api version in my Shopify extension TOML file to 2025-07 and updated the function target to cart.transform.run and the changed the merge operation to linesMerge as instructed by the documentation.

Lastly I was also instructed to regenerate my types using shopify app function typegen and to double check my input against the latest graphql scheme and none of that worked.

What should I do to resolve this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions