You are here

protected function OrderTotalWeightCondition::doEvaluate in Ubercart 8.4

Checks the weight of the order's products.

Parameters

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

string[] $products: The order products.

string $weight_units: The weight units.

float $weight_value: The weight value.

string $operator: The comparison operator.

Return value

bool TRUE if the order's weight meets the specified conditions.

File

uc_order/src/Plugin/Condition/OrderTotalWeightCondition.php, line 90

Class

OrderTotalWeightCondition
Provides 'Order total weight' condition.

Namespace

Drupal\uc_order\Plugin\Condition

Code

protected function doEvaluate(OrderInterface $order, array $products, $weight_units, $weight_value, $operator) {
  $totals = [
    'all' => 0,
  ];
  $total = 0;
  foreach ($order->products as $product) {
    $unit_conversion = uc_weight_conversion($product->weight_units, $weight_units);
    $totals['all'] += $product->qty * $product->weight * $unit_conversion;
    $totals[$product->nid] = $product->qty * $product->weight * $unit_conversion;
  }
  if (in_array('all', $products)) {
    $total = $totals['all'];
  }
  else {
    foreach ($products as $product) {
      if (isset($totals[$product])) {
        $total += $totals[$product];
      }
    }
  }
  return $this
    ->compareComparisonOptions($total, $operator, $weight_value);
}