You are here

function commerce_price_table_get_amount_qty in Commerce Price Table 7

Get the price for the min qty possible in a product.

1 call to commerce_price_table_get_amount_qty()
commerce_price_table_set_price in ./commerce_price_table.rules.inc
Rules callback: executes the "Replace the price for a price table" action.

File

./commerce_price_table.module, line 481

Code

function commerce_price_table_get_amount_qty($product, $quantity = 1, $items = array()) {
  if (empty($items)) {

    // Support legacy versions where rules doesn't send $items over.
    // Look up all price table items in the current product.
    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
    $fields = commerce_info_fields('commerce_price_table', 'commerce_product');
    foreach ($fields as $field) {
      if (!empty($product->{$field['field_name']})) {
        foreach ($product_wrapper->{$field['field_name']}
          ->value() as $item) {
          $items[] = $item;
        }
      }
    }
  }

  // Sort the items by quantity and return the matching one.
  uasort($items, 'commerce_price_table_sort_by_qty');
  foreach ($items as $item) {
    if ($quantity <= $item['max_qty'] && $quantity >= $item['min_qty']) {
      return $item;
    }
  }

  // Handle the unlimited qty.
  foreach ($items as $item) {
    if ($item['max_qty'] == -1) {
      return $item;
    }
  }

  // We fallback to the higher one if no match was found.
  return end($items);
}