old_friend

drupal - Price Resolver

Feb 25th, 2021
833
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace Drupal\commerce_product_saleprice\Resolvers;
  4.  
  5. use Drupal\commerce\Context;
  6. use Drupal\commerce\PurchasableEntityInterface;
  7. use Drupal\commerce_price\Price;
  8. use Drupal\commerce_price\Resolver\PriceResolverInterface;
  9.  
  10. /**
  11.  * Class SalePriceResolver.
  12.  *
  13.  * @package Drupal\commerce_product_saleprice\Resolvers
  14.  */
  15. class SalePriceResolver implements PriceResolverInterface
  16. {
  17.  
  18.   /**
  19.    * {@inheritdoc}
  20.    */
  21.   public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context)
  22.   {
  23.     // Make sure that product variation has a field called Sale price.
  24.     if (!$entity->hasField('field_sale_price')) {
  25.       return;
  26.     }
  27.  
  28.     if ($entity->get('field_sale_price')->isEmpty()) {
  29.       return;
  30.     }
  31.  
  32.     /** @var \Drupal\commerce_price\Price $sale_price */
  33.     $sale_price = $entity->get('field_sale_price')->first()->toPrice();
  34.     $sale_price_number = $sale_price->getNumber();
  35.     $sale_price_currency_code = $sale_price->getCurrencyCode();
  36.  
  37.     if (!$sale_price_number || $sale_price_number == 0) {
  38.       return;
  39.     }
  40.  
  41.     return new Price($sale_price_number, $sale_price_currency_code);
  42.   }
  43. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×