You are here

protected function BuyXGetY::removeQuantities in Commerce Core 8.2

Removes the second quantity list from the first quantity list.

For example: ['1' => '10', '2' => '20'] - ['1' => '10', '2' => '17'] will result in ['2' => '3'].

Parameters

array $first_quantities: The first quantity list.

array $second_quantities: The second quantity list.

Return value

array The new quantity list.

2 calls to BuyXGetY::removeQuantities()
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 726

Class

BuyXGetY
Provides the "Buy X Get Y" offer for orders.

Namespace

Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer

Code

protected function removeQuantities(array $first_quantities, array $second_quantities) {
  foreach ($second_quantities as $order_item_id => $quantity) {
    if (isset($first_quantities[$order_item_id])) {
      $first_quantities[$order_item_id] = Calculator::subtract($first_quantities[$order_item_id], $second_quantities[$order_item_id]);
      if ($first_quantities[$order_item_id] <= 0) {
        unset($first_quantities[$order_item_id]);
      }
    }
  }
  return $first_quantities;
}