You are here

public function DefaultPriceResolver::resolve in Price 2.0.x

Resolves a price for the given purchasable entity.

Use $context->getData('field_name', 'price') to get the name of the field for which the price is being resolved (e.g "price").

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity.

string $quantity: The quantity.

\Drupal\price\Context $context: The context.

Return value

\Drupal\price\Price|null A price value object, if resolved. Otherwise NULL, indicating that the next resolver in the chain should be called.

Overrides PriceResolverInterface::resolve

File

src/Resolver/DefaultPriceResolver.php, line 16

Class

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

Namespace

Drupal\price\Resolver

Code

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();
  }
}