Create a custom Liquid block for nested cart lines
This tutorial covers how to create nested cart lines for extended warranties using the Cart AJAX API and metafields with a custom Liquid block.
Anchor to 1. Create variant metafield definition for extended warranties1. Create variant metafield definition for extended warranties
First, you'll need to create a metafield definition that maps a product to their available extended warranty options.
Create a metafield definition named extended_warranty
on the product object and set the metafield type to a list of product variants.

This metafield will store references to warranty product variants that can be offered for each product variant. This can be configured on the Shopify admin product details page.

Anchor to 2. Custom Liquid block for creating nested cart line2. Custom Liquid block for creating nested cart line
Next, you'll create custom Liquid block that reads the extended warranty metafield and displays the available warranty options to customers.
On your theme editor, add a custom Liquid block on the product details page.

Paste the following snippet:
Custom Liquid block
Once saved, the block will be rendered on the product details page and the "Add to Cart" button will enable multiple product variants to be added to the cart, with selected variants containing a reference to the parent cart line.

When added to the cart, nested cart lines will have distinct representation on Checkout.

If you want to build validation to ensure that only eligible products were nested, the Cart and Checkout Validation Function can be leveraged.
Anchor to 3. Defining nesting rules3. Defining nesting rules
You can use custom data like custom data like product reference lists to define nesting rules. Depending on the use case, you can either:
- List all the products (or variants) a specific product (or variant) can be nested under.
- List all the products (or variants) that can be nested under a specific product (or variant).
This data can be read by:
- Themes in order to display the right products.
- Cart validation functions in order to validate the data before nesting the product under another one in the cart.
It is important to validate the cart using functions in order to ensure someone trying to manipulate the storefront using the cart API directly can't bypass the rules defined by the merchant through the app. Same goes for quantity and line splitting rules.
In this example, checkout and cart validation function has been deployed with an app for checking allowed nested cart lines. A metafield, nestable_products
contains a list of allowed products that can be nested.