You are here

public function AvailabilityManager::check in Commerce Core 8.2

Same name in this branch
  1. 8.2 src/AvailabilityManager.php \Drupal\commerce\AvailabilityManager::check()
  2. 8.2 modules/order/src/AvailabilityManager.php \Drupal\commerce_order\AvailabilityManager::check()

Checks the availability of the given order item.

Parameters

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

\Drupal\commerce\Context $context: The context.

Return value

\Drupal\commerce_order\AvailabilityResult An AvailabilityResult value object determining whether an order item is available for purchase.

Overrides AvailabilityManagerInterface::check

File

modules/order/src/AvailabilityManager.php, line 45

Class

AvailabilityManager
Default implementation of the availability manager.

Namespace

Drupal\commerce_order

Code

public function check(OrderItemInterface $order_item, Context $context) : AvailabilityResult {
  foreach ($this->checkers as $checker) {
    if (!$checker
      ->applies($order_item)) {
      continue;
    }
    $result = $checker
      ->check($order_item, $context);
    if ($result instanceof AvailabilityResult && $result
      ->isUnavailable()) {
      return $result;
    }
  }

  // Invoke the legacy checkers next, and wrap the return value into our
  // new AvailabilityResult value object.
  $purchased_entity = $order_item
    ->getPurchasedEntity();
  $quantity = $order_item
    ->getQuantity();
  foreach ($this->legacyCheckers as $checker) {
    @trigger_error(get_class($checker) . ' implements \\Drupal\\commerce\\AvailabilityCheckerInterface which is deprecated in commerce:8.x-2.18 and is removed from commerce:3.x. use \\Drupal\\commerce_order\\AvailabilityCheckerInterface instead.', E_USER_DEPRECATED);
    if (!$checker
      ->applies($purchased_entity)) {
      continue;
    }
    $result = $checker
      ->check($purchased_entity, $quantity, $context);
    if ($result === FALSE) {
      return AvailabilityResult::unavailable();
    }
  }
  return AvailabilityResult::neutral();
}