You are here

public function PriceListRepository::loadItems in Commerce Pricelist 8.2

Loads the price list items for the given purchasable entity.

Parameters

\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity.

\Drupal\commerce\Context $context: The context.

Return value

\Drupal\commerce_pricelist\Entity\PriceListItemInterface[] The price list items.

Overrides PriceListRepositoryInterface::loadItems

1 call to PriceListRepository::loadItems()
PriceListRepository::loadItem in src/PriceListRepository.php
Loads the price list item for the given purchasable entity.

File

src/PriceListRepository.php, line 66

Class

PriceListRepository

Namespace

Drupal\commerce_pricelist

Code

public function loadItems(PurchasableEntityInterface $entity, Context $context) {
  $customer_id = $context
    ->getCustomer()
    ->id();
  $store_id = $context
    ->getStore()
    ->id();
  $date = DrupalDateTime::createFromTimestamp($context
    ->getTime(), $context
    ->getStore()
    ->getTimezone());
  $now = $date
    ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
  $cache_key = implode(':', [
    $entity
      ->id(),
    $customer_id,
    $store_id,
    $now,
  ]);
  if (array_key_exists($cache_key, $this->priceListItems)) {
    return $this->priceListItems[$cache_key];
  }
  $price_list_ids = $this
    ->loadPriceListIds($entity
    ->getEntityTypeId(), $context);
  if (empty($price_list_ids)) {
    $this->priceListItems[$cache_key] = [];
    return [];
  }
  $price_list_item_storage = $this->entityTypeManager
    ->getStorage('commerce_pricelist_item');
  $query = $price_list_item_storage
    ->getQuery();
  $query
    ->condition('type', $entity
    ->getEntityTypeId())
    ->condition('price_list_id', $price_list_ids, 'IN')
    ->condition('purchasable_entity', $entity
    ->id())
    ->condition('status', TRUE)
    ->addTag('commerce_pricelist_item_query')
    ->addMetaData('price_list_ids', $price_list_ids)
    ->addMetaData('customer_id', $customer_id)
    ->addMetaData('store_id', $store_id)
    ->addMetaData('context', $context)
    ->sort('quantity', 'ASC');
  $result = $query
    ->execute();
  $price_list_items = [];
  if (!empty($result)) {
    $price_list_items = $price_list_item_storage
      ->loadMultiple($result);
  }
  $this->priceListItems[$cache_key] = $price_list_items;
  return $price_list_items;
}