2

I'm in the process of setting up a shop with Craft Commerce but I'm having some trouble organising the data structure of the products. The problem is that some products have both sizing and color options but I'm not sure how to enter these in the back-end. The most obvious way of doing this would be to make a product variant for each possible combination, like so:

Variant 1: color 1 - size 1
Variant 2: color 2 - size 1
Variant 3: color 1 - size 2
Variant 4: color 2 - size 2

I guess this would work but there will be a lot of variants. And if another one or two options are added in the future the amount of variants will grow exponentially.

Any insight on this would be much appreciated, thanks!

8

Usually when selling goods that come in different size and colors, a different SKU would exist for any size/color combination that you sell, thus a unique variant.

So, when that item becomes low in stock (Stock Keeping Unit), you can order more in.

In Craft Commerce we do not automatically generate every combination of variant based on the custom fields attached to a variant because most stores never sell every combination, and also, not every custom field is a unique aspect of that variant.

If more options are added to a product, there is nothing forcing you to make every combination as a variant, you could just use an option/custom field to mark a single variant as special (i.e Gold Edition).

If you are selling something that has a lot of options that customers are required to choose from each option, there is nothing stopping you from building a UI on the front end that makes selecting a variant possible based on all the options of all the custom fields. In that case your front end UI is building a query for a variant (craft.commerce.variants).

If you're not really selling a unique single SKU, but rather wanting a 'build a product' type product based on smaller SKUs, like building a bike, then you would need to build that yourself on the front end and/or with a plugin. That plugin would track what is in the cart, and which items still need to be added to complete the bike.

An alternative to that is building a custom purchasable with a plugin. This would allow you to expose the building of the purchasable element to the customer (with any options or custom logic you wish), and once the purchasable is saved by the customer, they can add it to the cart.

Lastly, if you don't want to go down the custom purchasable route, you can always just use generic options params when adding a product to the cart to allow any extra customer choices for the product. If the price is the same for the product regardless of the option chosen, this might be the easiest path if you have a huge amount of options and don't want to have a variant for each. The only thing you would want to do is validate the options for the current product.

The line item options feature mentioned above can also be combined with a small plugin that modifies the price of the line item based on the options choosen with the onPopulateLineItem event. The plugin would use the event something like this:

craft()->on('commerce_lineItems.onPopulateLineItem', function($event){

$purchasable= $event->params['purchasable'];
$lineItem = $event->params['lineItem'];

  if(isset($lineItem->options['giftWrapped']) && $lineItem->options['giftWrapped'] == 1){
    $lineItem->price = $lineItem->price + 1;
    $lineItem->salePrice = $lineItem->price;
    $lineItem->saleAmount = 0;
  }
}

Remembering that using the options method above will not be compatible with any stock tracking or inventory features now or in the future.

Let me know if you have any follow up questions and I can improve the answer above.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.