Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
// 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);
If the quantity is less than or equal to the maximum quantity and not greater than or equal to the lesser ... Then it automatically "-1"
Regards
Comment | File | Size | Author |
---|---|---|---|
remove_unnecessary_loop_in_commerce_price_table_get_amount_qty.patch | 503 bytes | facine |
Comments
Comment #1
facine CreditAttribution: facine commentedI forgot to change the status!
Comment #2
Cybso CreditAttribution: Cybso commentedNo, in this implementation the loop is necessary, because max_qty = -1 must not be at the end:
After uasort:
If $quantity = 17 (gap) or $quantity = 100, the loop would return item 1 (amount = 20), without the loop it would return item 3 (amount = 10).
Please have also a look at http://drupal.org/node/1897824, which tries to make the behavior of price table predictable on gapping intervals.
Comment #3
pcambraThanks for the insight Cybso, I'll mark this as dupe of the gapping one