public function DefaultPriceResolver::resolve in Commerce Core 8.2
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 "list_price", "price").
Parameters
\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity.
string $quantity: The quantity.
\Drupal\commerce\Context $context: The context.
Return value
\Drupal\commerce_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
- modules/
price/ src/ Resolver/ DefaultPriceResolver.php, line 16
Class
- DefaultPriceResolver
- Provides the default price, taking it directly from the purchasable entity.
Namespace
Drupal\commerce_price\ResolverCode
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();
}
}