You are here

protected function PriceListRepository::loadPriceListIds in Commerce Pricelist 8.2

Loads the available price list IDs for the given bundle and context.

Parameters

string $bundle: The price list bundle.

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

Return value

int[] The price list IDs.

2 calls to PriceListRepository::loadPriceListIds()
PriceListRepository::loadItem in src/PriceListRepository.php
Loads the price list item for the given purchasable entity.
PriceListRepository::loadItems in src/PriceListRepository.php
Loads the price list items for the given purchasable entity.

File

src/PriceListRepository.php, line 158

Class

PriceListRepository

Namespace

Drupal\commerce_pricelist

Code

protected function loadPriceListIds($bundle, 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(':', [
    $bundle,
    $customer_id,
    $store_id,
    $now,
  ]);
  if (array_key_exists($cache_key, $this->priceListIds)) {
    return $this->priceListIds[$cache_key];
  }
  $price_list_storage = $this->entityTypeManager
    ->getStorage('commerce_pricelist');
  $query = $price_list_storage
    ->getQuery();
  $query
    ->condition('type', $bundle)
    ->condition('stores', [
    $store_id,
  ], 'IN')
    ->condition($query
    ->orConditionGroup()
    ->condition('customers', $customer_id)
    ->notExists('customers'))
    ->condition($query
    ->orConditionGroup()
    ->condition('customer_roles', $context
    ->getCustomer()
    ->getRoles(), 'IN')
    ->notExists('customer_roles'))
    ->condition('start_date', $now, '<=')
    ->condition($query
    ->orConditionGroup()
    ->condition('end_date', $now, '>')
    ->notExists('end_date'))
    ->condition('status', TRUE)
    ->sort('weight', 'ASC')
    ->sort('id', 'DESC')
    ->addTag('commerce_pricelist_query')
    ->addMetaData('customer_id', $customer_id)
    ->addMetaData('store_id', $store_id)
    ->addMetaData('context', $context);
  $result = $query
    ->execute();
  $price_list_ids = array_values($result);
  $this->priceListIds[$cache_key] = $price_list_ids;
  return $price_list_ids;
}