You are here

function commerce_line_items_quantity in Commerce Core 7

Returns the total quantity of an array of line items.

Parameters

$line_items: The array of line items whose quantities you want to count; also accepts an EntityListWrapper of a line item reference field.

$types: An array of line item types to filter by before counting.

Return value

The total quantity of all the matching line items.

7 calls to commerce_line_items_quantity()
commerce_cart_block_view in modules/cart/commerce_cart.module
Implements hook_block_view().
commerce_cart_checkout_router in modules/cart/includes/commerce_cart.pages.inc
Redirects invalid checkout attempts or displays the checkout form if valid.
commerce_cart_menu_item_title in modules/cart/commerce_cart.module
Returns the title of the shopping cart menu item with an item count.
commerce_cart_view in modules/cart/includes/commerce_cart.pages.inc
Displays the shopping cart form and associated information.
commerce_line_item_handler_area_line_item_summary::render in modules/line_item/includes/views/handlers/commerce_line_item_handler_area_line_item_summary.inc
Render the area.

... See full list

File

modules/line_item/commerce_line_item.module, line 1453
Defines the core Commerce line item entity and API functions interact with line items on orders.

Code

function commerce_line_items_quantity($line_items, $types = array()) {

  // Sum up the quantity of all matching line items.
  $quantity = 0;
  foreach ($line_items as $line_item) {
    if ($line_item instanceof EntityMetadataWrapper) {
      $line_item = $line_item
        ->value();
    }
    if (empty($types) || in_array($line_item->type, $types)) {
      $quantity += $line_item->quantity;
    }
  }
  return $quantity;
}