You are here

public function BundlePriceResolver::resolve in Commerce Product Bundle 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/BundlePriceResolver.php, line 40

Class

BundlePriceResolver
Commerce Product Bundle Price Resolver.

Namespace

Drupal\commerce_product_bundle\Resolver

Code

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

  // We operate on product bundles only. Return fast if we have nothing to do.
  if (!$entity instanceof BundleInterface) {
    return NULL;
  }

  // In case the product bundle has a static price, we return that price.
  // Otherwise we compute a dynamic price from the bundle items.
  $price = $entity
    ->getPrice();
  if (!is_null($price)) {
    return $price;
  }
  else {
    $currency_code = $this->currentStore
      ->getStore()
      ->getDefaultCurrencyCode();
    $bundle_price = new Price('0.00', $currency_code);
    foreach ($entity
      ->getBundleItems() as $item) {
      $bundle_price = $bundle_price
        ->add($item
        ->getUnitPrice());
    }
    return $bundle_price;
  }
}