You are here

protected function OrderHasProductClassCondition::doEvaluate in Ubercart 8.4

Checks that the order has the selected combination of product classes.

Parameters

\Drupal\uc_order\OrderInterface $order: The order to check.

array $product_classes: An array of strings containing the product classes (node content types) to check against.

bool $required: TRUE to require all product classes be present in the order. FALSE to require at least one be present.

bool $forbidden: TRUE to require that only the listed product classes be present. FALSE to allow products with other classes.

Return value

bool TRUE if the order meets the specified conditions.

File

uc_order/src/Plugin/Condition/OrderHasProductClassCondition.php, line 134

Class

OrderHasProductClassCondition
Provides 'Order has a product with a selected product classes' condition.

Namespace

Drupal\uc_order\Plugin\Condition

Code

protected function doEvaluate(OrderInterface $order, array $product_classes = [], $required = FALSE, $forbidden = FALSE) {
  $order_product_classes = [];
  foreach ($order->products as $product) {
    if (!empty($product->type)) {

      // If present, use the product type from {uc_order_products}.data.type.
      $order_product_classes[] = $product->type;
    }
    else {

      // Otherwise, use the node type. If the node can't be loaded, ignore
      // this product.
      $node = Node::load($product->nid);
      if (!empty($node)) {
        $order_product_classes[] = $node->type;
      }
    }
  }
  $required_product_classes = array_intersect($product_classes, $order_product_classes);
  if ($required) {
    $required_check = $required_product_classes == $product_classes;
  }
  else {
    $required_check = (bool) count($required_product_classes);
  }
  if ($forbidden) {
    $forbidden_product_classes = array_diff($order_product_classes, $product_classes);
    $forbidden_check = (bool) count($forbidden_product_classes);
  }
  else {
    $forbidden_check = FALSE;
  }
  return $required_check && !$forbidden_check;
}