Cart Transform: Merge Lines Operation

Cart Transform: Merge Lines Operation

Combine multiple cart line items into a single bundled product

Overview

The Merge Lines block combines multiple cart line items from selected groups into a single bundled product. This is the opposite of the Expand operation — instead of breaking a bundle apart, it takes separate items and merges them together.

  • Build-your-own bundles — let customers select individual items that get merged into a single bundle at checkout
  • Kit assembly — combine prerequisite items into a bundled kit product
  • Multi-item promotions — merge qualifying items into a discounted bundle
  • Bundle builder apps — integrate with apps that tag cart lines with bundle identifiers at runtime

When cart lines are merged, all items from the selected groups are combined into a single line item represented by a bundle product (parent variant). The customer sees one bundled line item in their cart instead of the individual items.


Two Modes

The Merge Lines block supports two configuration modes:

  • Static mode — you select a fixed bundle product variant up front. All lines from the selected groups are merged into that one bundle. Best for simple, predictable bundles.
  • Dynamic bundle mode — the bundle product variant is determined at runtime by reading cart line properties, cart attributes, or metafields. Best for bundle builder apps and headless storefronts where bundles are assembled dynamically per cart. Dynamic mode itself offers two matching strategies: Lines share a bundle ID (lines with the same ID field value group together) and Variant-reference metafield (accessories self-declare their main variant via a metafield, no storefront code needed).

Setup

Open your cart transform function

Navigate to the function you want to edit in Function Studio

Add the Merge Lines block

Click Add Action and select Merge Lines from the action list

Configure the block

Set the options described in the Settings section below


Settings

Cart Line Groups

Required

Choose the cart line groups that should be merged into the bundle. Groups must be defined earlier in your function using a “Define Cart Line Groups” block. All lines from all selected groups are combined into the single bundle.

Dynamic Bundle

When turned off (default), a fixed bundle product variant is used — this is Static mode. When turned on, bundle grouping and the parent variant are determined at runtime. See Dynamic Bundle Mode below.

Bundle Product

Required in Static mode

Select the product variant that will represent the merged bundle in the cart. This is the “parent” product that replaces the individual items. The variant must exist and be published in your Shopify store.

Price Discount

Percentage discount to apply to the merged bundle (0–100%). Set to 0 for no discount.

This field can also be linked to a metafield, cart attribute, or line property. See Linked Fields.

Bundle Image

Optional

Custom image for the merged bundle product. Can be linked to a dynamic source.

Bundle Title

Optional

Custom display title for the merged bundle. Can be linked to a dynamic source.


Dynamic Bundle Mode

Dynamic bundle mode lets a single Merge Lines block handle any number of distinct bundles in the cart at runtime. Instead of merging all lines into one fixed bundle, the block reads a field on each cart line to decide which lines belong together.

When to Use Dynamic Mode

  • Your storefront or a bundle builder app sets line properties like _bundle_id on each cart line
  • Multiple different bundles can exist in the same cart simultaneously
  • The parent product variant varies from bundle to bundle and cannot be known ahead of time

Enabling Dynamic Mode

Toggle Dynamic bundle on in the block settings. The static Bundle Product variant picker is replaced by the dynamic bundle settings below.


Bundle Matching

When Dynamic bundle is on, a Bundle matching dropdown appears. It controls how cart lines are paired into bundles at runtime. Two modes are available:

Lines share a bundle ID (default)

Lines are grouped by a shared identifier field. See Shared ID mode settings below.

Variant-reference metafield pairs two items

Lines are grouped around a shared target variant using a variant-reference metafield. No hand-maintained bundle ID is needed — each accessory variant simply declares which main variant it belongs to. See Variant-reference mode settings below.

Note

Variant-reference mode is only available when your cart transform function uses the metafields input type. If you do not see this option in the dropdown, check the input type of your function.


Shared ID Mode Settings

Bundle ID Source

Required

Link this field to a cart attribute, line property, or metafield whose value groups cart lines into bundles. Lines that share the same resolved value are merged together. Lines where this field is empty or unresolvable are skipped.

Parent Variant Resolution

Controls how the parent (bundle product) variant ID is determined for each bundle group. Choose one of two modes:

Field Contains Variant ID

The field linked in “Parent variant source” resolves directly to a Shopify variant ID (e.g., gid://shopify/ProductVariant/123456). This value is used as the parent variant.

Field Marks the Parent Item

The field linked in “Parent variant source” is a boolean marker. The line within the bundle group where this field’s value is "1" or "true" is treated as the parent line — its product variant is automatically used as the bundle’s parent variant ID.

Parent Variant Source

Required

Link this field to the cart attribute, line property, or metafield that holds either the variant ID or the parent marker, depending on the resolution mode chosen above.

Expected Component Count

Optional

Enter the number of items you expect to share the same bundle ID to form a complete bundle. If the actual number of lines does not match, that entire bundle group is skipped. Leave blank to merge any group regardless of size.

Tip

Use expected component count to guard against incomplete bundles being merged prematurely. For example, set to 5 if a valid bundle always contains exactly 5 items.

How Shared ID Bundles Are Processed

Read cart lines

The block reads all cart lines from the selected groups

Group by bundle ID

Each line’s Bundle ID source field is resolved. Lines with the same value are placed into the same group

Skip incomplete groups

Lines with an empty or unresolvable bundle ID are ignored. If Expected component count is set and the group does not match, the group is skipped

Resolve parent variant

The parent variant ID is resolved for each group according to the selected resolution mode

Merge

Each qualifying group becomes one merged bundle in the cart


Variant-Reference Mode Settings

This mode solves a specific problem: you want to automatically bundle a main product with one or more accessories, without maintaining a separate bundle ID on every cart line. Instead, each accessory variant carries a metafield that points directly at the main variant it belongs with. When the main variant is in the cart, the block assembles the bundle automatically.

Pair Variant Reference

Required — metafield link only

Link this field to a variant-reference metafield on the cart line’s variant. The metafield’s value must be the Shopify ID of another product variant (the target / main variant). All cart lines whose metafield resolves to the same target variant are grouped together with the target line and merged into one bundle.

The Price discount field is evaluated on every line in the bundle — the highest resolved value wins. Bundle title and Bundle image are resolved from the target (main) variant’s line.

How Variant-Reference Bundles Are Processed

Read cart lines

The block reads all cart lines from the selected groups

Resolve references

For each line, the Pair variant reference metafield is read. Lines where the metafield is empty or unset are not pointer lines

Group by target variant

Pointer lines that resolve to the same target variant are placed into the same candidate bundle

Check target is present

If the target variant is not also present as a cart line, no bundle is formed for that group

Merge

The target line(s) and all pointer lines become one merged bundle. The target variant is used as the parent variant

Apply discount

The Price discount is evaluated on every line in the bundle; the maximum value is applied

Note

Each cart line can participate in at most one bundle. If a line would match more than one bundle, it is assigned to the first one and skipped for any subsequent ones.


Setting Up Variant-Reference Mode: Step by Step

Create a variant metafield definition in Shopify admin

Go to Settings > Custom data > Variants and add a new metafield definition. Set the type to Product variant (this creates a variant-reference field). Give it a name you will recognize, such as “Bundles with”. Pin it so it appears on the variant edit page

Tag your accessory variants

Open each accessory variant in the Shopify admin and set the new metafield to point at the main product variant it should bundle with. Leave the metafield blank on the main variant itself

Open your cart transform function in Function Studio

Navigate to the function you want to configure (it must use the metafields input type)

Add a Merge Lines block

Click Add Action and select Merge Lines

Turn on Dynamic bundle

Toggle the Dynamic bundle switch on

Select the match mode

In the Bundle matching dropdown, choose Variant-reference metafield pairs two items

Under Pair variant reference, click “Select a field” and pick the variant metafield definition you created in step 1

Configure Price discount, Bundle title, and Bundle image

These can be static values or linked to metafields as needed

Save and publish your function


Choosing Between Shared ID and Variant-Reference

SituationRecommended mode
Your storefront or app already sets a bundle ID on cart linesLines share a bundle ID
You need strict component count validationLines share a bundle ID
You want accessories to self-declare their main product via a metafield, with no storefront code changesVariant-reference metafield
Your function uses a non-metafield input typeLines share a bundle ID (variant-reference is not available)
You have a main product that can be bundled with multiple different accessories simultaneouslyVariant-reference metafield

Linked Fields

Three fields on the Merge Lines block can be either typed as a static value or linked to a dynamic source (metafield, cart attribute, or line property).

Note

Linked fields are gated behind the Advanced plan. The Dynamic bundle mode fields (Bundle ID source, Parent variant source) are link-only fields and always require an Advanced plan.

When a field is linked, a link icon and badge show which source it is connected to. Clicking the edit button lets you change the linked field. Clicking the unlink button removes the link and restores the static input.

Price Discount

By default, type a static percentage (0–100). Alternatively, link to a numeric metafield, cart attribute, or line property. At runtime, the discount percentage is read from the linked field for each bundle.

Bundle Title

By default, type a static title. Alternatively, link to a string metafield, cart attribute, or line property. Useful when each bundle should display a title set by your storefront or app.

Bundle Image

By default, provide a static image URL via the image picker. Alternatively, link to a string field containing an image URL. The URL is resolved at runtime and used as the bundle’s line item image.


How Merge Works

When the function runs, it collects all cart lines from the selected groups and replaces them with bundled line items:

  1. All cart lines matching the selected groups are collected
  2. Each line’s quantity is preserved as-is
  3. In static mode, all collected lines are replaced by one bundle line item
  4. In dynamic mode, lines are grouped by their resolved bundle ID, each group becoming its own bundle
  5. If a price discount is configured, it is applied as a percentage decrease to each bundle’s total
  6. Optional image and title override the default bundle product presentation

Quantities

Each merged cart line retains its original quantity. For example, if a group contains a T-shirt (qty 2) and a Cap (qty 1), the merge operation includes both items with their respective quantities inside the bundle.

Pricing

The bundle’s price is derived from the sum of the original prices of the merged cart lines:

  • No discount (0%) — bundle price equals the sum of original item prices
  • With discount (e.g., 10%) — bundle price = total of original prices minus 10%

Examples

Build-Your-Own Bundle (Static)

A customer picks 3 items from a “Build Your Bundle” collection, and they get merged into a single bundle product with a 15% discount.

Create a cart line group

Add a “Define Cart Line Groups” block with group name BUNDLE_PICKS that matches items from the bundle collection

Add the Merge Lines block

Set Cart Line Groups to BUNDLE_PICKS, select your “Custom Bundle” variant as the Bundle Product, set Price Discount to 15, and optionally set a Bundle Title and Bundle Image

Result

The 3 individual items are replaced by a single “Your Custom Bundle” line item with a 15% discount applied

Starter Kit Assembly (Static)

When a customer adds a laptop, case, and charger to their cart, merge them into a “Starter Kit” bundle.

Create a cart line group

Add a “Define Cart Line Groups” block with group name KIT_ITEMS that matches the laptop, case, and charger variants

Add the Merge Lines block

Set Cart Line Groups to KIT_ITEMS, select your “Laptop Starter Kit” variant, and set Price Discount to 0

Result

Individual items are consolidated into a single “Laptop Starter Kit” line item

Multi-Group Meal Deal (Static)

Merge items from multiple groups — a main course group and a sides group — into a single “Meal Deal” bundle.

Create cart line groups

Add two “Define Cart Line Groups” blocks — MAIN_COURSE for main course items and SIDES for side dish items

Add the Merge Lines block

Set Cart Line Groups to both MAIN_COURSE and SIDES, select your “Meal Deal” variant, set Price Discount to 20, and set Bundle Title to Meal Deal

Result

All items from both groups are merged into one “Meal Deal” bundle with a 20% discount

Bundle Builder App Integration (Dynamic)

A bundle builder app on your storefront lets customers assemble custom bundles. The app sets _bundle_id and _is_bundle_parent line properties on cart lines.

Create a cart line group

Add a group that captures all bundle lines (e.g., by matching a tag or line property indicating they are part of a bundle)

Configure the Merge Lines block

Turn on Dynamic bundle. Link Bundle ID source to the line property _bundle_id. Set Parent variant resolution to “Field marks the parent item”. Link Parent variant source to _is_bundle_parent. Set Price Discount to 10

Result

Each distinct _bundle_id value becomes its own merged bundle. The parent line’s product variant becomes the bundle’s parent variant. Each bundle receives a 10% discount

Fixed-Size Bundle Validation (Dynamic)

Your storefront sells pre-configured 3-item bundles. Each bundle must have exactly 3 components or it should not merge.

Create a cart line group

Add a group capturing all bundle-tagged lines

Configure the Merge Lines block

Turn on Dynamic bundle. Link Bundle ID source to the field holding the bundle ID. Set Parent variant resolution to “Field contains variant ID”. Link Parent variant source to the field holding the parent variant GID. Set Expected component count to 3

Result

Only bundle groups with exactly 3 lines are merged. Groups with fewer or more lines are left as individual items

Camera and Accessories Bundle (Variant-Reference)

You sell cameras and accessories (lens, bag, memory card). Any combination of accessories should automatically bundle with the camera when both are in the cart — without any storefront code changes.

Create a variant metafield

In Shopify admin under Settings > Custom data > Variants, add a metafield definition of type Product variant named “Bundles with”. Pin it on your variant pages

Tag each accessory variant

Open the lens variant, bag variant, and memory card variant. For each one, set “Bundles with” to point at the camera variant

Create a cart line group

Add a “Define Cart Line Groups” block with a group name ALL_LINES that captures all cart lines (or a group that captures camera and accessory lines specifically)

Configure the Merge Lines block

Set Cart Line Groups to ALL_LINES. Turn on Dynamic bundle. In Bundle matching, select “Variant-reference metafield pairs two items”. Under Pair variant reference, link to the “Bundles with” metafield. Set Price Discount to 10

Result

When a camera and any accessories are in the cart together, they are merged into one bundle with a 10% discount. Accessories not paired with a camera in the cart remain as individual lines


Best Practices

Planning Your Merge Strategy

  • Decide which items should be mergeable and create appropriate cart line groups
  • Use Function Studio conditions to only run the merge when certain criteria are met
  • Test with different cart configurations to verify correct grouping

Bundle Product Setup (Static Mode)

  • Create a dedicated product variant to represent the bundle
  • Set appropriate product images and descriptions for the bundle variant
  • Consider inventory management — the bundle variant typically should not track inventory if the individual components do

Dynamic Bundle Setup

  • Work with your storefront developer or bundle builder app documentation to understand which line properties or attributes are set on cart lines
  • Use Expected component count to guard against incomplete bundles being merged prematurely (Shared ID mode only)
  • Test with carts containing multiple simultaneous bundles to confirm each group resolves independently
  • For variant-reference mode, verify your metafield definition type is Product variant (not a plain text field) — the block reads a variant reference, not a text string
  • In variant-reference mode, the target variant must be present in the cart for a bundle to form; if only accessories are added, no merge occurs

Pricing Strategy

  • Use percentage discounts to incentivize bundles over individual purchases
  • Set 0% discount if the bundle is purely for presentation and organization
  • When using linked fields for the discount, ensure the linked field always resolves to a valid number between 0 and 100

Customer Experience

  • Use meaningful bundle titles so customers understand what they are purchasing
  • Provide a bundle image that represents the combined items
  • The total price reflects the bundle pricing, but individual component prices are not visible to customers
  • Test the checkout experience with merged bundles

Important Considerations

Warning

Shopify rejects merge operations if any of the cart lines being merged have a selling plan (subscription) attached. Make sure your groups exclude subscription items.

Note

Linking Price Discount, Bundle Title, or Bundle Image to a dynamic source requires the Advanced plan. The Dynamic bundle mode fields are link-only and always require the Advanced plan.

Store Requirements

  • The bundle product variant must exist and be published in your store (static mode)
  • In dynamic mode, the parent variant ID is resolved at runtime — ensure all possible variants are published

Cart Behavior

  • All individual items from the selected groups are replaced by a single bundle line item per resolved group
  • Original item quantities are preserved within the merge operation
  • Cart totals reflect the bundle pricing (with any configured discount)
  • Customers see one line item per bundle
  • Individual component prices are not visible to customers

Function Flow

  • Cart line groups must be defined before the merge block
  • Once items are merged, subsequent blocks targeting the original individual items will not find them
  • Place the merge block after group definitions but before discount blocks

Troubleshooting

Merge Not Working

  • Check your groups — make sure the cart line groups are defined correctly before the merge block
  • Verify bundle variant — in static mode, ensure the bundle product variant exists and is published
  • Check group contents — verify that your groups actually contain cart lines (empty groups cause the merge to skip)

Bundle Not Appearing

  • Check group matching — verify your cart items match the group conditions
  • Review bundle product — ensure the bundle product variant is available and not archived
  • Check selling plans — merge is rejected if any cart line has a selling plan attached

Dynamic Bundle Not Grouping Correctly (Shared ID Mode)

  • Check the Bundle ID source — confirm the linked field is actually being set on the cart lines by your storefront or app
  • Check line property key spelling — keys are case-sensitive (_bundle_id and _Bundle_Id are different)
  • Verify parent marker value — in “Field marks the parent item” mode, the parent field must resolve to "1" or "true"
  • Check component count — if Expected component count is set and the group does not match, it is silently skipped. Try removing the count restriction temporarily
Tip

If dynamic bundles are not merging, try removing the Expected component count restriction temporarily to isolate whether the issue is with grouping or with count validation.

Variant-Reference Bundle Not Merging

  • Check the metafield type — the Pair variant reference field must be linked to a Product variant type metafield, not a text or number metafield
  • Check the metafield is set on the accessory variant — open the accessory variant in Shopify admin and confirm the metafield value is set to the correct main variant
  • Confirm the main variant is in the cart — if only accessories are added without the main variant, no bundle is formed
  • Check your function input type — variant-reference mode is only available for cart transform functions with the metafields input type. If the Bundle matching dropdown does not show this option, the function uses a different input type
  • Check your cart line group — the group must include both the main variant’s line and the accessory lines. If the group only captures accessories, the main variant line will not be part of the merge

Pricing Issues

  • Verify discount range — confirm the percentage is correct
  • Check linked discount field — if linked, confirm the field resolves to a numeric value at runtime
  • Check for conflicts — ensure no other blocks are modifying the same cart lines after the merge

Cart Display Issues

  • Clear your cart — remove test items and try again with a fresh cart
  • Check the bundle product — ensure the bundle product has proper images and titles in Shopify
  • Review bundle settings — verify the optional title and image are correct