You are here

public function PriceListDefaultBasePriceResolver::resolve in Commerce Pricelist 8

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

src/Resolver/PriceListDefaultBasePriceResolver.php, line 19

Class

PriceListDefaultBasePriceResolver
Class PriceListDefaultBasePriceResolver.

Namespace

Drupal\commerce_pricelist\Resolver

Code

public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {

  // Make sure that product variation has a field called Saleprice.
  if (!$entity
    ->hasField('bundle_price')) {
    return;
  }
  if ($entity
    ->get('bundle_price')
    ->isEmpty()) {
    return;
  }

  /** @var \Drupal\commerce_price\Price $bundle_price */
  $bundle_price = $entity
    ->get('bundle_price')
    ->first()
    ->toPrice();
  $bundle_price_number = $bundle_price
    ->getNumber();
  $bundle_price_currency_code = $bundle_price
    ->getCurrencyCode();
  if (!$bundle_price_number || $bundle_price_currency_code == 0) {
    return;
  }
  return new Price($bundle_price_number, $bundle_price_currency_code);
}