public function TestPriceResolver::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/ tests/ modules/ commerce_price_test/ src/ TestPriceResolver.php, line 19
Class
- TestPriceResolver
- Test price resolver.
Namespace
Drupal\commerce_price_testCode
public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {
if (!($entity instanceof ProductVariationInterface && strpos($entity
->getSku(), 'TEST_') !== FALSE)) {
return NULL;
}
$field_name = $context
->getData('field_name', 'price');
if ($entity
->hasField($field_name) && !$entity
->get($field_name)
->isEmpty()) {
/** @var \Drupal\commerce_price\Price $price */
$price = $entity
->get($field_name)
->first()
->toPrice();
return $price
->subtract(new Price('3', $price
->getCurrencyCode()));
}
}