Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\my_prices\Plugin\Field\FieldFormatter;
- use Drupal\commerce\Context;
- use Drupal\commerce_order\Plugin\Field\FieldFormatter\PriceCalculatedFormatter;
- use Drupal\Core\Cache\Cache;
- use Drupal\Core\Field\Annotation\FieldFormatter;
- use Drupal\Core\Field\FieldItemListInterface;
- use Drupal\Core\Language\LanguageInterface;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\views\Plugin\views\field\EntityField;
- use Drupal\commerce\PurchasableEntityInterface;
- /**
- * Plugin implementation of the 'commerce_price_calculated' formatter.
- *
- * @FieldFormatter(
- * id = "commerce_cart_price_calculated",
- * label = @Translation("Cart Price Calculated"),
- * field_types = {
- * "commerce_price"
- * }
- * )
- */
- class CartPriceCalculatedFormatter extends PriceCalculatedFormatter
- {
- public function viewElements(FieldItemListInterface $items, $langcode)
- {
- $elements = [];
- if (!$items->isEmpty()) {
- $context = new Context($this->currentUser, $this->currentStore->getStore(), NULL, [
- 'field_name' => $items->getName(),
- ]);
- /** @var 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);
- $currency_code = $result->getCalculatedPrice()->getCurrencyCode();
- $options = $this->getFormattingOptions();
- // get retail (base), wholesale and promo prices
- $price = $purchasable_entity->getPrice()->getNumber();
- $price_opt = $purchasable_entity->get('field_price_opt')->first()->toPrice()->getNumber();
- $price_promo = $purchasable_entity->get('field_sale_price')->first()->toPrice()->getNumber();
- // get actual (dinamic) price of goods (for comparison with the static prices (see above))
- $current_store = \Drupal::service('commerce_store.current_store');
- $store = $current_store->getStore();
- $cart_provider = \Drupal::service('commerce_cart.cart_provider');
- $cart = $cart_provider->getCart("physical", $store);
- $cart_items = $cart->getItems();
- global $my_prices_id_item;
- if ($my_prices_id_item == null) {
- $my_prices_id_item = 0;
- }
- $actual_price = (float)$cart_items[$my_prices_id_item]->get('unit_price')->getString();
- $my_prices_id_item++;
- if ($price_promo > 0) {
- $discountedPriceFormatter = $this->currencyFormatter->format($price_promo, $currency_code, $options);
- $originalPriceFormatter = $this->currencyFormatter->format($price, $currency_code, $options);
- $markup = sprintf('<span class="product-price-sale">%s</span> <span class="product-price-base"><del>%s</del></span>', $discountedPriceFormatter, $originalPriceFormatter);
- } else if ($price_opt == $actual_price) {
- $discountedPriceFormatter = $this->currencyFormatter->format($price_opt, $currency_code, $options);
- $originalPriceFormatter = $this->currencyFormatter->format($price, $currency_code, $options);
- $markup = sprintf('<span class="product-price-sale">%s</span> <span class="product-price-base"><del>%s</del></span>', $discountedPriceFormatter, $originalPriceFormatter);
- } else {
- $discountedPriceFormatter = $this->currencyFormatter->format($price, $currency_code, $options);
- $markup = sprintf('%s', $discountedPriceFormatter);
- }
- $elements[0] = [
- '#markup' => $markup,
- '#cache' => [
- 'tags' => $purchasable_entity->getCacheTags(),
- 'contexts' => Cache::mergeContexts($purchasable_entity->getCacheContexts(), [
- 'languages:' . LanguageInterface::TYPE_INTERFACE,
- 'country',
- ]),
- ],
- ];
- }
- return $elements;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement