Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save laryn/7a652afd5227f226ea174d8e8c5b8f08 to your computer and use it in GitHub Desktop.
Save laryn/7a652afd5227f226ea174d8e8c5b8f08 to your computer and use it in GitHub Desktop.
Setting a global default size and color in Drupal Commerce product displays.
/*********************************************************************************
* Setting a global default size and color in Drupal Commerce product displays.
*
* Include this in your custom module (renaming the function appropriately),
* it is not a full custom module on its own.
*********************************************************************************/
function MYMODULE_commerce_product_reference_default_delta_alter(&$delta, $products) {
$firstproduct = reset($products);
// You can specify a particular product type below
if ($firstproduct->type == 'product_type_machine_name') {
foreach ($products as $key=>$product) {
// Replace field names, COLORCODE and SIZECODE below according to your
// site. This checks for a specific size and color and sets it as the
// default selection in the product display.
if (($product->field_color['und'][0]['value'] == 'COLORCODE') &&
($product->field_size['und'][0]['value'] == 'SIZECODE')) {
// Drupal Commerce returns either the array key or product id as delta so we
// need to check if array key 0 exists, otherwise use product ID.
if (isset($products[0])) {
$delta = $key;
} else {
$delta = $product->product_id;
}
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment