You are here

commerce_stocked_default.module in Commerce Stocked Default 7

Ensure that the default product on the add to cart form is a stocked item.

File

commerce_stocked_default.module
View source
<?php

/**
 * @file
 * Ensure that the default product on the add to cart form is a stocked item.
 */

/**
 * Implements hook_commerce_product_reference_default_delta_alter().
 */
function commerce_stocked_default_commerce_product_reference_default_delta_alter(&$delta, &$products) {
  if (count($products) <= 1) {
    return;
  }
  foreach ($products as $product_delta => $product) {
    $stock_level = field_get_items('commerce_product', $product, 'commerce_stock');
    $stock_level = $stock_level[0]['value'];

    // Default to the first item in the list which is stocked. This will default
    // to the order that admins defines, simply skipping unstocked items.
    if ($stock_level >= 1) {
      $delta = $product_delta;
      break;
    }
  }
}