You are here

function commerce_physical_order_weight in Commerce Physical Product 7

Determines the weight of an entire order.

Parameters

commerce_order $order: The order object whose weight value should be calculated.

string $unit: The unit of measurement to use for the returned weight of the order.

Return value

array A weight field value array representing the total weight of the order using the specified unit of measurement or NULL if no weight could be determined.

1 call to commerce_physical_order_weight()
commerce_physical_rules_order_weight_comparison in ./commerce_physical.rules.inc
Calculates the order's total weight and performs a comparison on it.

File

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

Code

function commerce_physical_order_weight($order, $unit = 'lb') {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $weight = NULL;

  // Loop over each line item on the order.
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {

    // Get the weight value of product line items.
    if (in_array($line_item_wrapper
      ->getBundle(), commerce_product_line_item_types())) {
      $line_item_weight = commerce_physical_product_line_item_weight($line_item_wrapper
        ->value());

      // Add it to the running total converting it to the required weight unit.
      if (!empty($line_item_weight['weight'])) {
        $converted_weight = physical_weight_convert($line_item_weight, $unit);
        if (empty($weight['weight'])) {
          $weight = $converted_weight;
        }
        else {
          $weight['weight'] += $converted_weight['weight'];
        }
      }
    }
  }

  // Allow other modules to alter the weight if necessary.
  drupal_alter('commerce_physical_order_weight', $weight, $order, $unit);
  return $weight;
}