Commit d006d195 authored by Lisa Streeter's avatar Lisa Streeter Committed by Bojan Živanović
Browse files

Issue #2805549 by lisastreeter, edurenye, mglaman, flocondetoile, bojanz:...

Issue #2805549 by lisastreeter, edurenye, mglaman, flocondetoile, bojanz: Expand the "Calculated price" formatter with the ability to show prices with promotions/taxes/fees
parent 034b3a9d
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -5,10 +5,11 @@
 * Defines the Order entity and associated features.
 */

use Drupal\entity\BundleFieldDefinition;
use Drupal\commerce_order\Entity\OrderTypeInterface;
use Drupal\commerce_order\Plugin\Field\FieldFormatter\PriceCalculatedFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\entity\BundleFieldDefinition;

/**
 * Implements hook_theme().
@@ -56,6 +57,17 @@ function commerce_order_local_tasks_alter(&$definitions) {
  }
}

/**
 * Implements hook_field_formatter_info_alter().
 *
 * Replaces the commerce_price PriceCalculatedFormatter with
 * the expanded commerce_order one.
 */
function commerce_order_field_formatter_info_alter(array &$info) {
  $info['commerce_price_calculated']['class'] = PriceCalculatedFormatter::class;
  $info['commerce_price_calculated']['provider'] = 'commerce_order';
}

/**
 * Implements hook_field_widget_form_alter().
 *
+4 −0
Original line number Diff line number Diff line
@@ -62,3 +62,7 @@ services:
    arguments: ['@current_route_match']
    tags:
      - { name: commerce_store.store_resolver, priority: 100 }

  commerce_order.price_calculator:
    class: Drupal\commerce_order\PriceCalculator
    arguments: ['@entity_type.manager', '@commerce_price.chain_price_resolver', '@commerce_order.chain_order_type_resolver', '@request_stack']
+21 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_order;

use Drupal\commerce_order\DependencyInjection\Compiler\PriceCalculatorPass;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;

/**
 * Registers the PriceCalculator compiler pass.
 */
class CommerceOrderServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritdoc}
   */
  public function register(ContainerBuilder $container) {
    $container->addCompilerPass(new PriceCalculatorPass());
  }

}
+54 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_order\DependencyInjection\Compiler;

use Drupal\commerce_order\OrderProcessorInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Adds order processors to the PriceCalculator, grouped by adjustment type.
 */
class PriceCalculatorPass implements CompilerPassInterface {

  /**
   * {@inheritdoc}
   */
  public function process(ContainerBuilder $container) {
    $definition = $container->getDefinition('commerce_order.price_calculator');
    $processor_interface = OrderProcessorInterface::class;
    $processors = [];
    foreach ($container->findTaggedServiceIds('commerce_order.order_processor') as $id => $attributes) {
      $processor = $container->getDefinition($id);
      if (!is_subclass_of($processor->getClass(), $processor_interface)) {
        throw new LogicException("Service '$id' does not implement $processor_interface.");
      }
      $attribute = $attributes[0];
      if (empty($attribute['adjustment_type'])) {
        continue;
      }

      $processors[$id] = [
        'priority' => isset($attribute['priority']) ? $attribute['priority'] : 0,
        'adjustment_type' => $attribute['adjustment_type'],
      ];
    }

    // Sort the processors by priority.
    uasort($processors, function ($processor1, $processor2) {
      if ($processor1['priority'] == $processor2['priority']) {
        return 0;
      }
      return ($processor1['priority'] > $processor2['priority']) ? -1 : 1;
    });

    // Add the processors to PriceCalculator.
    foreach ($processors as $id => $processor) {
      $arguments = [new Reference($id), $processor['adjustment_type']];
      $definition->addMethodCall('addProcessor', $arguments);
    }
  }

}
+211 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_order\Plugin\Field\FieldFormatter;

use Drupal\commerce\Context;
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\commerce_order\AdjustmentTypeManager;
use Drupal\commerce_order\PriceCalculatorInterface;
use Drupal\commerce_price\NumberFormatterFactoryInterface;
use Drupal\commerce_price\Plugin\Field\FieldFormatter\PriceDefaultFormatter;
use Drupal\commerce_store\CurrentStoreInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Alternative implementation of the 'commerce_price_calculated' formatter.
 *
 * @see \Drupal\commerce_price\Plugin\Field\FieldFormatter\PriceCalculatedFormatter
 */
class PriceCalculatedFormatter extends PriceDefaultFormatter implements ContainerFactoryPluginInterface {

  /**
   * The adjustment type manager.
   *
   * @var \Drupal\commerce_order\AdjustmentTypeManager
   */
  protected $adjustmentTypeManager;

  /**
   * The currency storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $currencyStorage;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The current store.
   *
   * @var \Drupal\commerce_store\CurrentStoreInterface
   */
  protected $currentStore;

  /**
   * The price calculator.
   *
   * @var \Drupal\commerce_order\PriceCalculatorInterface
   */
  protected $priceCalculator;

  /**
   * Constructs a new PriceCalculatedFormatter object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings settings.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce_price\NumberFormatterFactoryInterface $number_formatter_factory
   *   The number formatter factory.
   * @param \Drupal\commerce_order\AdjustmentTypeManager $adjustment_type_manager
   *   The adjustment type manager.
   * @param \Drupal\commerce_store\CurrentStoreInterface $current_store
   *   The current store.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\commerce_order\PriceCalculatorInterface $price_calculator
   *   The price calculator.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, NumberFormatterFactoryInterface $number_formatter_factory, AdjustmentTypeManager $adjustment_type_manager, CurrentStoreInterface $current_store, AccountInterface $current_user, PriceCalculatorInterface $price_calculator) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $entity_type_manager, $number_formatter_factory);

    $this->adjustmentTypeManager = $adjustment_type_manager;
    $this->currencyStorage = $entity_type_manager->getStorage('commerce_currency');
    $this->currentStore = $current_store;
    $this->currentUser = $current_user;
    $this->priceCalculator = $price_calculator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $plugin_id,
      $plugin_definition,
      $configuration['field_definition'],
      $configuration['settings'],
      $configuration['label'],
      $configuration['view_mode'],
      $configuration['third_party_settings'],
      $container->get('entity_type.manager'),
      $container->get('commerce_price.number_formatter_factory'),
      $container->get('plugin.manager.commerce_adjustment_type'),
      $container->get('commerce_store.current_store'),
      $container->get('current_user'),
      $container->get('commerce_order.price_calculator')
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'adjustment_types' => [],
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = parent::settingsForm($form, $form_state);

    $elements['adjustment_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Adjustments'),
      '#options' => [],
      '#default_value' => $this->getSetting('adjustment_types'),
    ];
    foreach ($this->adjustmentTypeManager->getDefinitions() as $plugin_id => $definition) {
      if (!in_array($plugin_id, ['custom'])) {
        $label = $this->t('Apply @label to the calculated price', ['@label' => $definition['plural_label']]);
        $elements['adjustment_types']['#options'][$plugin_id] = $label;
      }
    }

    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
    $enabled_adjustment_types = array_filter($this->getSetting('adjustment_types'));
    foreach ($this->adjustmentTypeManager->getDefinitions() as $plugin_id => $definition) {
      if (in_array($plugin_id, $enabled_adjustment_types)) {
        $summary[] = $this->t('Apply @label to the calculated price', ['@label' => $definition['plural_label']]);
      }
    }

    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    if (!$items->isEmpty()) {
      $context = new Context($this->currentUser, $this->currentStore->getStore());
      /** @var \Drupal\commerce\PurchasableEntityInterface $purchasable_entity */
      $purchasable_entity = $items->getEntity();
      $adjustment_types = array_filter($this->getSetting('adjustment_types'));
      $result = $this->priceCalculator->calculate($purchasable_entity, 1, $context, $adjustment_types);
      /** @var \Drupal\commerce_price\Price $calculated_price */
      $calculated_price = $result['calculated_price'];
      $number = $calculated_price->getNumber();
      /** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
      $currency = $this->currencyStorage->load($calculated_price->getCurrencyCode());

      $elements[0] = [
        '#markup' => $this->numberFormatter->formatCurrency($number, $currency),
        '#cache' => [
          'tags' => $purchasable_entity->getCacheTags(),
          'contexts' => Cache::mergeContexts($purchasable_entity->getCacheContexts(), [
            'languages:' . LanguageInterface::TYPE_INTERFACE,
            'country',
          ]),
        ],
      ];
    }

    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    $entity_type = \Drupal::entityTypeManager()->getDefinition($field_definition->getTargetEntityTypeId());
    return $entity_type->entityClassImplements(PurchasableEntityInterface::class);
  }

}
Loading