You are here

public function PriceListPriceResolver::resolve in Commerce Pricelist 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

src/PriceListPriceResolver.php, line 31

Class

PriceListPriceResolver

Namespace

Drupal\commerce_pricelist

Code

public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {
  $price = NULL;
  $price_list_item = $this->priceListRepository
    ->loadItem($entity, $quantity, $context);
  if ($price_list_item) {
    $field_name = $context
      ->getData('field_name', 'price');
    if ($field_name == 'list_price') {
      $price = $price_list_item
        ->getListPrice();
    }
    elseif ($field_name == 'price') {
      $price = $price_list_item
        ->getPrice();
    }
  }
  return $price;
}