Vote up!
Vote down!

How to auto Sort Product Variations in the Products Display?

I have a products display with product variations, say red mug, blue mug, green etc.

I cannot sort this within views, I only have Fields: 'Content: Rendered Content' to work with. Not sure where to go to sort variations before they are finally listed in the product view. I can only edit the product variations manually and move a none-zero stock item to the top.

Currently people think 'red is sold out, and disregard 'blue' and 'green' because message 'sold old' is 'to strong' and they don't realize there is more. After complaints of too many 'variation buttons' in the product label seen in the product Display.

I think auto sort of product variations using 'stock = descending' in the Product Display keeps is better to increase sales. How do I auto sort variations in products displays such that the non-zero (blue or green in my sample) is shown when red is sold out?

Hope someone can give a suggestion.

Asked by: Rob
on May 22, 2015

1 Answer

Vote up!
Vote down!

I really wondered what the answer to this was, and eventually found a solution, thanks to a comment by rsrama on http://fdiv.net/2014/06/11/sorting-products-drupal-commerce-add-cart-form

This sets the default product to be the first variant that has a stock level greater than zero.

function HOOK_commerce_product_reference_default_delta_alter(&$delta, $products) {
foreach ($products as $key => $product) {
$p_wrapper = entity_metadata_wrapper('commerce_product', $product);
$stock = $p_wrapper->commerce_stock->value();
if ($stock > 0) {
$delta = $key;

Andy @ BlueFusion
Posted: May 27, 2015