You are here

class PriceResolverPriceTable in Commerce Price Table 8

Price resolver alter the base price.

Hierarchy

Expanded class hierarchy of PriceResolverPriceTable

1 string reference to 'PriceResolverPriceTable'
commerce_price_table.services.yml in ./commerce_price_table.services.yml
commerce_price_table.services.yml
1 service uses PriceResolverPriceTable
commerce_price_table.price_resolver_price_table in ./commerce_price_table.services.yml
Drupal\commerce_price_table\Resolvers\PriceResolverPriceTable

File

src/Resolvers/PriceResolverPriceTable.php, line 14

Namespace

Drupal\commerce_price_table\Resolvers
View source
class PriceResolverPriceTable implements PriceResolverInterface {

  /**
   * {@inheritdoc}
   */
  public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {
    foreach ($entity
      ->getFieldDefinitions() as $name => $field) {
      $types[] = $field
        ->getType();
      if ($field
        ->getType() == 'commerce_price_table') {
        $price = $this
          ->getTablePrice($entity
          ->get($name), $quantity);
        if ($price) {
          $entity
            ->setPrice($price);
        }
      }
    }
  }

  /**
   * Get Price object depending on quantity.
   *
   * @return \Drupal\commerce_price\Price|NULL
   */
  public function getTablePrice(FieldItemList $values, $quantity) {
    foreach ($values as $item) {
      if ($quantity >= $item->min_qty and ($quantity <= $item->max_qty or $item->max_qty == -1)) {
        return new Price($item->amount, $item->currency_code);
      }
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PriceResolverPriceTable::getTablePrice public function Get Price object depending on quantity.
PriceResolverPriceTable::resolve public function Resolves a price for the given purchasable entity. Overrides PriceResolverInterface::resolve