You are here

protected function BuyXGetY::buildAdjustmentAmount in Commerce Core 8.2

Builds an adjustment amount for the given order item and quantity.

Parameters

\Drupal\commerce_order\Entity\OrderItemInterface $order_item: The order item.

string $quantity: The quantity.

Return value

\Drupal\commerce_price\Price The adjustment amount.

1 call to BuyXGetY::buildAdjustmentAmount()
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 779

Class

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

Namespace

Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer

Code

protected function buildAdjustmentAmount(OrderItemInterface $order_item, $quantity) {
  if ($this->configuration['offer_type'] == 'percentage') {
    $percentage = (string) $this->configuration['offer_percentage'];
    $total_price = $order_item
      ->getTotalPrice();
    if ($order_item
      ->getQuantity() != $quantity) {

      // Calculate a new total for just the quantity that will be discounted.
      $total_price = $order_item
        ->getUnitPrice()
        ->multiply($quantity);
      $total_price = $this->rounder
        ->round($total_price);
    }
    $adjustment_amount = $total_price
      ->multiply($percentage);
  }
  else {
    $amount = Price::fromArray($this->configuration['offer_amount']);
    $adjustment_amount = $amount
      ->multiply($quantity);
  }
  $adjustment_amount = $this->rounder
    ->round($adjustment_amount);
  return $adjustment_amount;
}