You are here

protected function BuyXGetY::mergeQuantities in Commerce Core 8.2

Merges the first quantity list with the second quantity list.

Quantities belonging to shared order item IDs will be added together.

For example: ['1' => '10'] and ['1' => '10', '2' => '17'] will merge into ['1' => '20', '2' => '17'].

Parameters

array $first_quantities: The first quantity list.

array $second_quantities: The second quantity list.

Return value

array The new quantity list.

1 call to BuyXGetY::mergeQuantities()
BuyXGetY::apply in modules/promotion/src/Plugin/Commerce/PromotionOffer/BuyXGetY.php
Applies the offer to the given entity.

File

modules/promotion/src/Plugin/Commerce/PromotionOffer/BuyXGetY.php, line 755

Class

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

Namespace

Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer

Code

protected function mergeQuantities(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] = $quantity;
    }
    else {
      $first_quantities[$order_item_id] = Calculator::add($first_quantities[$order_item_id], $second_quantities[$order_item_id]);
    }
  }
  return $first_quantities;
}