Skip to content

Shopify Dude Fix

Shopify Cart Quantity Updates Change the Wrong Item? Use Line Item Keys

Target individual cart lines safely when the same variant appears more than once.

Quick answer: Use the current line item key with cart/change.js, not the product variant ID.
On this page

Why this happens

A personalized item and its plain counterpart can share a variant ID while occupying separate lines. A quantity control that targets only the variant can therefore modify the wrong purchase.

Implementation

Render each cart row with its own Liquid item.key in an escaped data attribute. Send the current key as the string id and the desired integer quantity to the locale-aware change endpoint. Serialize mutations and rebuild the cart controls from the returned cart after every successful change.

Example

const response = await fetch(window.Shopify.routes.root + 'cart/change.js', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({id:currentKey, quantity:2})});
if (!response.ok) throw new Error('Cart update failed');
const cart = await response.json();

On failure, reload cart state before offering another attempt. Do not keep old keys in local storage or retry against a guessed variant.

Common misunderstanding: A line key is not a permanent identifier. Discounts and property changes can replace it.
How to test this:

  1. Add two copies of one variant with different properties
  2. change only the second row
  3. remove the first
  4. apply a discount and refresh the keys
  5. repeat under a translated storefront.
Sources and further reading: Shopify reference ยท Shopify implementation documentation