protected function BuyXGetY::sliceQuantities in Commerce Core 8.2
Takes a slice from the given quantity list.
For example, ['1' => '10', '2' => '5'] sliced for total quantity '11' will produce a ['1' => '10', '2' => '1'] slice, leaving ['2' => '4'] in the original list.
Parameters
array $quantities: The quantity list. Modified by reference.
string $total_quantity: The total quantity of the new slice.
Return value
array The quantity list slice.
2 calls to BuyXGetY::sliceQuantities()
- BuyXGetY::apply in modules/
promotion/ src/ Plugin/ Commerce/ PromotionOffer/ BuyXGetY.php - Applies the offer to the given entity.
- BuyXGetY::calculateExpectedGetQuantity in modules/
promotion/ src/ Plugin/ Commerce/ PromotionOffer/ BuyXGetY.php - Calculates the expected get quantity.
File
- modules/
promotion/ src/ Plugin/ Commerce/ PromotionOffer/ BuyXGetY.php, line 690
Class
- BuyXGetY
- Provides the "Buy X Get Y" offer for orders.
Namespace
Drupal\commerce_promotion\Plugin\Commerce\PromotionOfferCode
protected function sliceQuantities(array &$quantities, $total_quantity) {
$remaining_quantity = $total_quantity;
$slice = [];
foreach ($quantities as $order_item_id => $quantity) {
if ($quantity <= $remaining_quantity) {
$remaining_quantity = Calculator::subtract($remaining_quantity, $quantity);
$slice[$order_item_id] = $quantity;
unset($quantities[$order_item_id]);
if ($remaining_quantity === '0') {
break;
}
}
else {
$slice[$order_item_id] = $remaining_quantity;
$quantities[$order_item_id] = Calculator::subtract($quantity, $remaining_quantity);
break;
}
}
return $slice;
}