You are here

class DefaultPriceResolver in Price 2.0.x

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

Hierarchy

Expanded class hierarchy of DefaultPriceResolver

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

File

src/Resolver/DefaultPriceResolver.php, line 11

Namespace

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

  /**
   * {@inheritdoc}
   */
  public function resolve(ContentEntityInterface $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