You are here

class PriceListDefaultBasePriceResolver in Commerce Pricelist 8

Class PriceListDefaultBasePriceResolver.

@package Drupal\commerce_pricelist

Hierarchy

Expanded class hierarchy of PriceListDefaultBasePriceResolver

1 string reference to 'PriceListDefaultBasePriceResolver'
commerce_pricelist.services.yml in ./commerce_pricelist.services.yml
commerce_pricelist.services.yml
1 service uses PriceListDefaultBasePriceResolver
commerce_pricelist.default_base_price_resolver in ./commerce_pricelist.services.yml
Drupal\commerce_pricelist\Resolver\PriceListDefaultBasePriceResolver

File

src/Resolver/PriceListDefaultBasePriceResolver.php, line 14

Namespace

Drupal\commerce_pricelist\Resolver
View source
class PriceListDefaultBasePriceResolver implements PriceListPriceResolverInterface {

  /**
   * {@inheritdoc}
   */
  public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {

    // Make sure that product variation has a field called Saleprice.
    if (!$entity
      ->hasField('bundle_price')) {
      return;
    }
    if ($entity
      ->get('bundle_price')
      ->isEmpty()) {
      return;
    }

    /** @var \Drupal\commerce_price\Price $bundle_price */
    $bundle_price = $entity
      ->get('bundle_price')
      ->first()
      ->toPrice();
    $bundle_price_number = $bundle_price
      ->getNumber();
    $bundle_price_currency_code = $bundle_price
      ->getCurrencyCode();
    if (!$bundle_price_number || $bundle_price_currency_code == 0) {
      return;
    }
    return new Price($bundle_price_number, $bundle_price_currency_code);
  }

  /**
   * @param \Drupal\commerce\PurchasableEntityInterface $entity
   *   The purchasable entity.
   * @return \Drupal\commerce_price\Price
   *   The purchasable price.
   * @throws \Drupal\Core\TypedData\Exception\MissingDataException
   */
  public function getPrice(PurchasableEntityInterface $entity) {
    $type_id = $entity
      ->getEntityType()
      ->id();
    $currency_code = \Drupal::service('commerce_store.current_store')
      ->getStore()
      ->getDefaultCurrencyCode();
    $price = new Price('0.00', $currency_code);
    if ($type_id == 'commerce_product_bundle') {
      if (!$entity
        ->get('bundle_price')
        ->isEmpty() && !$entity
        ->get('bundle_price')
        ->first()
        ->toPrice()
        ->isZero()) {
        $price = $entity
          ->get('bundle_price')
          ->first()
          ->toPrice();
      }
    }
    else {
      if (!$entity
        ->getPrice()
        ->isZero()) {
        $price = $entity
          ->getPrice();
      }
    }
    return $price;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PriceListDefaultBasePriceResolver::getPrice public function
PriceListDefaultBasePriceResolver::resolve public function Resolves a price for the given purchasable entity. Overrides PriceResolverInterface::resolve