Advertisement
old_friend

drupal 8 price formatter

Apr 17th, 2021 (edited)
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.86 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. use Drupal\Core\Form\FormStateInterface;
  12. use Drupal\views\Plugin\views\field\EntityField;
  13. use Drupal\commerce\PurchasableEntityInterface;
  14.  
  15. /**
  16.  * Plugin implementation of the 'commerce_price_calculated' formatter.
  17.  *
  18.  * @FieldFormatter(
  19.  *   id = "commerce_cart_price_calculated",
  20.  *   label = @Translation("Cart Price Calculated"),
  21.  *   field_types = {
  22.  *     "commerce_price"
  23.  *   }
  24.  * )
  25.  */
  26. class CartPriceCalculatedFormatter extends PriceCalculatedFormatter
  27. {
  28.  
  29.   public function viewElements(FieldItemListInterface $items, $langcode)
  30.   {
  31.     $elements = [];
  32.     if (!$items->isEmpty()) {
  33.       $context = new Context($this->currentUser, $this->currentStore->getStore(), NULL, [
  34.         'field_name' => $items->getName(),
  35.       ]);
  36.       /** @var PurchasableEntityInterface $purchasable_entity */
  37.       $purchasable_entity = $items->getEntity();
  38.       $adjustment_types = array_filter($this->getSetting('adjustment_types'));
  39.  
  40.       $result = $this->priceCalculator->calculate($purchasable_entity, 1, $context, $adjustment_types);
  41.       $currency_code = $result->getCalculatedPrice()->getCurrencyCode();
  42.       $options = $this->getFormattingOptions();
  43.       //  get retail (base), wholesale and promo prices
  44.       $price = $purchasable_entity->getPrice()->getNumber();
  45.       $price_opt = $purchasable_entity->get('field_price_opt')->first()->toPrice()->getNumber();
  46.       $price_promo = $purchasable_entity->get('field_sale_price')->first()->toPrice()->getNumber();
  47.  
  48.       // get actual (dinamic) price of goods (for comparison with the static prices (see above))
  49.       $current_store = \Drupal::service('commerce_store.current_store');
  50.       $store = $current_store->getStore();
  51.       $cart_provider = \Drupal::service('commerce_cart.cart_provider');
  52.       $cart = $cart_provider->getCart("physical", $store);
  53.       $cart_items = $cart->getItems();
  54.      
  55.       global $my_prices_id_item;
  56.       if ($my_prices_id_item == null) {
  57.         $my_prices_id_item = 0;
  58.       }
  59.       $actual_price = (float)$cart_items[$my_prices_id_item]->get('unit_price')->getString();
  60.       $my_prices_id_item++;
  61.  
  62.       if ($price_promo > 0) {
  63.         $discountedPriceFormatter = $this->currencyFormatter->format($price_promo, $currency_code, $options);
  64.         $originalPriceFormatter = $this->currencyFormatter->format($price, $currency_code, $options);
  65.  
  66.         $markup = sprintf('<span class="product-price-sale">%s</span> <span class="product-price-base"><del>%s</del></span>', $discountedPriceFormatter, $originalPriceFormatter);
  67.       } else if ($price_opt == $actual_price) {
  68.  
  69.         $discountedPriceFormatter = $this->currencyFormatter->format($price_opt, $currency_code, $options);
  70.         $originalPriceFormatter = $this->currencyFormatter->format($price, $currency_code, $options);
  71.  
  72.         $markup = sprintf('<span class="product-price-sale">%s</span> <span class="product-price-base"><del>%s</del></span>', $discountedPriceFormatter, $originalPriceFormatter);
  73.       } else {
  74.         $discountedPriceFormatter = $this->currencyFormatter->format($price, $currency_code, $options);
  75.         $markup = sprintf('%s', $discountedPriceFormatter);
  76.       }
  77.  
  78.       $elements[0] = [
  79.         '#markup' => $markup,
  80.         '#cache' => [
  81.           'tags' => $purchasable_entity->getCacheTags(),
  82.           'contexts' => Cache::mergeContexts($purchasable_entity->getCacheContexts(), [
  83.             'languages:' . LanguageInterface::TYPE_INTERFACE,
  84.             'country',
  85.           ]),
  86.         ],
  87.       ];
  88.     }
  89.  
  90.     return $elements;
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement