Merx

Product Variants

If you want to sell shoes you probably want to sell the shoes in a specific size. This cookbook shows you how to sell variations of your products.

Add to cart

Add the desired variations to the customer’s cart. That’s it.

$cart->add([
  'id' => 'nice-shoes',
  'key' => 'nice-shoes-abc',
  'color' => 'red',
  'size' => 38,
]);

You can even set a custom price within the add method:

$cart->add([
  'id' => 'nice-shoes',
  'key' => 'nice-shoes-xyc',
  'color' => 'gold',
  'size' => 41.5,
  'price' => 249.99,
]);

Tax is calculated automatically based on the tax value defined in the product page.

If you add the same product to the cart, the cart item will be merged/overwritten by default (the quantity is added up). To add more than one variant of a product to the cart, you have to set a unique key for each variant.

Combination with stock management

If you want to combine product variations with stock management things are getting more complicated. We recommend to think of product variations as own products.

content/nice-shoes/variant-red-41/product.txt

Title: Nice shoes
----
Price: 99.99
----
Tax: 19
----
Color: red
----
Size: 41
----
Stock: 10

content/nice-shoes/variant-red-40/product.txt

Title: Nice shoes
----
Price: 99.99
----
Tax: 19
----
Color: red
----
Size: 40
----
Stock: 8

content/nice-shoes/variant-gold-40/product.txt

Title: Nice shoes
----
Price: 249.99
----
Tax: 19
----
Color: gold
----
Size: 41
----
Stock: 1

Add variant to cart

$cart->add([
  'id' => 'nice-shoes/variant-gold-40',
]);