You are here

public function XquantityOrderItem::getQuantityPrice in Commerce Extended Quantity 8

File

src/Entity/XquantityOrderItem.php, line 195

Class

XquantityOrderItem
Overrides the order item entity class.

Namespace

Drupal\commerce_xquantity\Entity

Code

public function getQuantityPrice(FormInterface $form_object, PurchasableEntityInterface $purchased_entity, $quantity = NULL) {
  $price = NULL;
  if (isset($form_object->quantityPrices) && ($qty_prices = $form_object->quantityPrices)) {
    $scale = $form_object->quantityScale ?: 0;
    $product = $purchased_entity
      ->getProduct();
    $product_stores = $product
      ->getStores();
    array_walk($product_stores, function (&$store) {
      $store = $store
        ->bundle();
    });
    $data = [
      'variation_id' => $purchased_entity
        ->id(),
      'variation_type' => $purchased_entity
        ->bundle(),
      'product_id' => $product
        ->id(),
      'product_type' => $product
        ->bundle(),
      'list_price' => $purchased_entity
        ->getListPrice(),
      'product_stores' => $product_stores,
      'current_roles' => \Drupal::currentUser()
        ->getRoles(),
    ];
    foreach ($qty_prices as $qty_price) {
      $start = bccomp($qty_price['qty_start'], $quantity, $scale);
      $end = $qty_price['qty_end'] ? bccomp($quantity, $qty_price['qty_end'], $scale) : 0;
      if ($end === 1 || $start === 1) {
        continue;
      }
      if ($this
        ->quantityPriceApplies($qty_price, $data)) {
        $price = $qty_price['price'];
      }
    }
  }
  return $price;
}