You are here

class OrderTotalSummary in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/order/src/OrderTotalSummary.php \Drupal\commerce_order\OrderTotalSummary
  2. 8.2 modules/order/src/Plugin/Field/FieldFormatter/OrderTotalSummary.php \Drupal\commerce_order\Plugin\Field\FieldFormatter\OrderTotalSummary

Hierarchy

Expanded class hierarchy of OrderTotalSummary

1 string reference to 'OrderTotalSummary'
commerce_order.services.yml in modules/order/commerce_order.services.yml
modules/order/commerce_order.services.yml
1 service uses OrderTotalSummary
commerce_order.order_total_summary in modules/order/commerce_order.services.yml
Drupal\commerce_order\OrderTotalSummary

File

modules/order/src/OrderTotalSummary.php, line 7

Namespace

Drupal\commerce_order
View source
class OrderTotalSummary implements OrderTotalSummaryInterface {

  /**
   * The adjustment transformer.
   *
   * @var \Drupal\commerce_order\AdjustmentTransformerInterface
   */
  protected $adjustmentTransformer;

  /**
   * Constructs a new OrderTotalSummary object.
   *
   * @param \Drupal\commerce_order\AdjustmentTransformerInterface $adjustment_transformer
   *   The adjustment transformer.
   */
  public function __construct(AdjustmentTransformerInterface $adjustment_transformer) {
    $this->adjustmentTransformer = $adjustment_transformer;
  }

  /**
   * {@inheritdoc}
   */
  public function buildTotals(OrderInterface $order) {
    $adjustments = $order
      ->collectAdjustments();
    $adjustments = $this->adjustmentTransformer
      ->processAdjustments($adjustments);

    // Included adjustments are not displayed to the customer, they
    // exist to allow the developer to know what the price is made of.
    // The one exception is taxes, which need to be shown for legal reasons.
    $adjustments = array_filter($adjustments, function (Adjustment $adjustment) {
      return $adjustment
        ->getType() == 'tax' || !$adjustment
        ->isIncluded();
    });

    // Convert the adjustments to arrays.
    $adjustments = array_map(function (Adjustment $adjustment) {
      return $adjustment
        ->toArray();
    }, $adjustments);

    // Provide the "total" key for backwards compatibility reasons.
    foreach ($adjustments as $index => $adjustment) {
      $adjustments[$index]['total'] = $adjustments[$index]['amount'];
    }
    return [
      'subtotal' => $order
        ->getSubtotalPrice(),
      'adjustments' => $adjustments,
      'total' => $order
        ->getTotalPrice(),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OrderTotalSummary::$adjustmentTransformer protected property The adjustment transformer.
OrderTotalSummary::buildTotals public function Builds the totals for the given order. Overrides OrderTotalSummaryInterface::buildTotals
OrderTotalSummary::__construct public function Constructs a new OrderTotalSummary object.