You are here

function commerce_physical_product_line_item_dimensions in Commerce Physical Product 7

Determines the dimensions of a product line item on an order.

Parameters

commerce_line_item $line_item: A product line item whose dimensions should be determined.

Return value

array An array of dimensions field value arrays. There'll be one entry in the array per product, with the entry being an array of that product's dimensions. If this line item contains no products with dimensions, an empty array will be returned.

2 calls to commerce_physical_product_line_item_dimensions()
commerce_physical_order_dimensions in ./commerce_physical.module
Determines the dimensions of each product in an entire order.
commerce_physical_order_volume in ./commerce_physical.module
Determines the volume of an entire order.

File

./commerce_physical.module, line 138
API for working with physical product types in Drupal Commerce.

Code

function commerce_physical_product_line_item_dimensions($line_item) {
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $dimensions = array();

  // If the line item references a valid product...
  if (!empty($line_item_wrapper->commerce_product)) {
    $product = $line_item_wrapper->commerce_product
      ->value();
    if (!empty($product)) {

      // If the product has a valid dimensions field...
      $field_name = commerce_physical_entity_dimensions_field_name('commerce_product', $product);
      if (!empty($field_name) && !empty($product->{$field_name})) {
        $product_wrapper = entity_metadata_wrapper('commerce_product', $product);

        // Add dimension values per product in the line item.
        for ($i = 0; $i < $line_item_wrapper->quantity
          ->value(); $i++) {
          $dimensions[] = $product_wrapper->{$field_name}
            ->value();
        }
      }
    }
  }

  // Allow other modules to alter the weight if necessary.
  drupal_alter('commerce_physical_product_line_item_dimensions', $dimensions, $line_item);
  return $dimensions;
}