Skip to content

Discount function · Plan: Advanced

How do I discount accessories only when the matching main product is in the cart, and only as many as there are main products?

Link the items through a product or variant metafield, condition the accessory lines on that link, and use aggregates and a variable to cap the discounted quantity at the number of main products in the cart. This needs the Advanced plan, not Shopify Plus.

  • Function typeDiscount
  • Plan neededAdvanced
  • Shopify PlusNot required
  • Updated

What the shopper sees

They add two lamps. Any compatible bulbs they add are 25% off, up to two bulbs. A third bulb is full price. Remove a lamp and only one bulb stays discounted.

How the rule is built

  1. Link the products. Put a metafield on each bulb, for example custom.fits_socket = E27, and the same on each lamp. Or store a list of compatible bulb variant ids on the lamp.
  2. Data access. Select that product metafield for the function.
  3. Count the main products. Define a variable lamps as the quantity sum of lines tagged lamp (or in the Lamps collection).
  4. Rule. Product discount, line conditions tag = bulb AND metafield fits_socket equals <the lamp's value>, target selection cheapest matching, target quantity lamps, value 25%.

The cap comes from the target quantity being driven by the variable, so the discount scales with the number of lamps and never beyond it.

Variations

  • Several socket types in one cart. One rule per socket type, each with its own variable.
  • Free instead of 25%. Set the value to 100%.
  • Only when bought together for the first time. Add a customer segment on the discount’s eligibility.

Limits to know

  • Variables are an Advanced plan feature.
  • The match is by metafield value equality, so keep values normalized (E27, not E 27 on some products).

Related offers

Build this on your store

Install NERD Functions, start from a template, and test it against a real cart before it goes live. 7-day free trial.