Advertisement
old_friend

drupal - formatter

Feb 25th, 2021
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\my_prices\Plugin\Field\FieldFormatter;
  4.  
  5. use Drupal\commerce\Context;
  6. use Drupal\commerce_order\Plugin\Field\FieldFormatter\PriceCalculatedFormatter;
  7. use Drupal\Core\Cache\Cache;
  8. use Drupal\Core\Field\Annotation\FieldFormatter;
  9. use Drupal\Core\Field\FieldItemListInterface;
  10. use Drupal\Core\Language\LanguageInterface;
  11.  
  12. /**
  13.  * Plugin implementation of the 'commerce_price_calculated' formatter.
  14.  *
  15.  * @FieldFormatter(
  16.  *   id = "commerce_price_calculated_discounted",
  17.  *   label = @Translation("Calculated discounted"),
  18.  *   field_types = {
  19.  *     "commerce_price"
  20.  *   }
  21.  * )
  22.  */
  23. class PriceCalculatedDiscountedFormatter extends PriceCalculatedFormatter
  24. {
  25.  
  26.   public function viewElements(FieldItemListInterface $items, $langcode)
  27.   {
  28.     $elements = [];
  29.     if (!$items->isEmpty()) {
  30.       $context = new Context($this->currentUser, $this->currentStore->getStore(), NULL, [
  31.         'field_name' => $items->getName(),
  32.       ]);
  33.       /** @var \Drupal\commerce\PurchasableEntityInterface $purchasable_entity */
  34.       $purchasable_entity = $items->getEntity();
  35.       $adjustment_types = array_filter($this->getSetting('adjustment_types'));
  36.  
  37.       $result = $this->priceCalculator->calculate($purchasable_entity, 1, $context, $adjustment_types);
  38.       $currency_code = $result->getCalculatedPrice()->getCurrencyCode();
  39.       $options = $this->getFormattingOptions();
  40.  
  41.       $number = $result->getCalculatedPrice()->getNumber();
  42.    
  43.       $discountedPriceFormatter = $this->currencyFormatter->format($number, $currency_code, $options);
  44.  
  45.       $price = $purchasable_entity->getPrice()->getNumber();
  46.       $originalPriceFormatter = $this->currencyFormatter->format($price, $currency_code, $options);
  47.  
  48.       if ($number == $price) {
  49.         $markup = sprintf('%s', $discountedPriceFormatter);
  50.       } else {
  51.         $markup = sprintf('<span class="product-price-sale">%s</span> <span class="product-price-base"><del>%s</del></span>', $discountedPriceFormatter, $originalPriceFormatter);
  52.       }
  53.  
  54.       $elements[0] = [
  55.         '#markup' => $markup,
  56.         '#cache' => [
  57.           'tags' => $purchasable_entity->getCacheTags(),
  58.           'contexts' => Cache::mergeContexts($purchasable_entity->getCacheContexts(), [
  59.             'languages:' . LanguageInterface::TYPE_INTERFACE,
  60.             'country',
  61.           ]),
  62.         ],
  63.       ];
  64.     }
  65.  
  66.     return $elements;
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement