You are here

class DefaultPriceResolver in Commerce Core 8.2

Provides the default price, taking it directly from the purchasable entity.

Hierarchy

Expanded class hierarchy of DefaultPriceResolver

1 string reference to 'DefaultPriceResolver'
commerce_price.services.yml in modules/price/commerce_price.services.yml
modules/price/commerce_price.services.yml
1 service uses DefaultPriceResolver
commerce_price.default_price_resolver in modules/price/commerce_price.services.yml
Drupal\commerce_price\Resolver\DefaultPriceResolver

File

modules/price/src/Resolver/DefaultPriceResolver.php, line 11

Namespace

Drupal\commerce_price\Resolver
View source
class DefaultPriceResolver implements PriceResolverInterface {

  /**
   * {@inheritdoc}
   */
  public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {
    $field_name = $context
      ->getData('field_name', 'price');
    if ($field_name == 'price') {

      // Use the price getter to allow custom purchasable entity types to have
      // computed prices that are not backed by a field called "price".
      return $entity
        ->getPrice();
    }
    elseif ($entity
      ->hasField($field_name) && !$entity
      ->get($field_name)
      ->isEmpty()) {
      return $entity
        ->get($field_name)
        ->first()
        ->toPrice();
    }
  }

}

Members

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