OrderTotal.php in Commerce Core 8.2
File
modules/order/src/Plugin/views/area/OrderTotal.php
View source
<?php
namespace Drupal\commerce_order\Plugin\views\area;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\area\AreaPluginBase;
use Drupal\views\Plugin\views\argument\NumericArgument;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrderTotal extends AreaPluginBase {
protected $orderStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->orderStorage = $entity_type_manager
->getStorage('commerce_order');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['empty']['#description'] = $this
->t("Even if selected, this area handler will never render if a valid order cannot be found in the View's arguments.");
}
public function render($empty = FALSE) {
if (!$empty || !empty($this->options['empty'])) {
foreach ($this->view->argument as $name => $argument) {
if (!$argument instanceof NumericArgument) {
continue;
}
if (!in_array($argument
->getField(), [
'commerce_order.order_id',
'commerce_order_item.order_id',
])) {
continue;
}
if ($order = $this->orderStorage
->load($argument
->getValue())) {
$order_total = $order
->get('total_price')
->view([
'label' => 'hidden',
'type' => 'commerce_order_total_summary',
'weight' => $this->position,
]);
$order_total['#prefix'] = '<div data-drupal-selector="order-total-summary">';
$order_total['#suffix'] = '</div>';
return $order_total;
}
}
}
return [];
}
}