You are here

public function OrderOtherCount::render in Commerce add to cart confirmation 1.x

Render the area.

Parameters

bool $empty: (optional) Indicator if view result is empty or not. Defaults to FALSE.

Return value

array In any case we need a valid Drupal render array to return.

Overrides AreaPluginBase::render

File

src/Plugin/views/area/OrderOtherCount.php, line 80

Class

OrderOtherCount
Defines an order total area handler.

Namespace

Drupal\commerce_add_to_cart_confirmation\Plugin\views\area

Code

public function render($empty = FALSE) {
  if (!$empty || !empty($this->options['empty'])) {
    foreach ($this->view->argument as $argument) {

      // First look for an order_id argument.
      if (!$argument instanceof NumericArgument) {
        continue;
      }
      if ($argument
        ->getField() !== 'commerce_order_item.order_item_id') {
        continue;
      }

      /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
      $order_item = $this->orderItemStorage
        ->load($argument
        ->getValue());
      if (!$order_item) {
        continue;
      }
      if ($order = $order_item
        ->getOrder()) {
        $current_order_item_quantity = $order_item
          ->getQuantity();
        $total_order_quantity = 0;
        $other_order_price = $order
          ->getTotalPrice()
          ->subtract($order_item
          ->getTotalPrice());
        foreach ($order
          ->getItems() as $item) {
          $total_order_quantity += $item
            ->getQuantity();
        }
        $other_order_quantity = $total_order_quantity - $current_order_item_quantity;
        if (!$other_order_quantity) {
          return [];
        }
        $other_title = \Drupal::translation()
          ->formatPlural($other_order_quantity, '1 other item in your Cart', '@count other items in your Cart');
        return [
          '#type' => 'inline_template',
          '#template' => '<div class="order-other"> {{ title }} <div class="price"> {{ price|commerce_price_format }}</div></div>',
          '#context' => [
            'title' => $other_title,
            'price' => $other_order_price,
          ],
        ];
      }
    }
  }
  return [];
}