You are here

function _commerce_reports_stock_get_stock_enabled_products in Commerce Reporting 7.4

Get the stock enabled products.

Parameters

bool $reset: Boolean to reset cache.

Return value

array Array of products, keyed by SKU.

1 call to _commerce_reports_stock_get_stock_enabled_products()
_commerce_reports_stock_calculate_dataset in modules/stock/commerce_reports_stock.module
Calculate all the stock reports data.

File

modules/stock/commerce_reports_stock.module, line 189
This module provides advanced stock reporting for Drupal Commerce.

Code

function _commerce_reports_stock_get_stock_enabled_products($reset = FALSE) {
  $product_list =& drupal_static(__FUNCTION__);

  // If there is no static cache for dataset yet or a reset was specified...
  if (!isset($product_list) || $reset) {
    $product_list = array();

    // Grab all products that have stock enabled.
    $product_query = new EntityFieldQuery();
    $product_query
      ->entityCondition('entity_type', 'commerce_product')
      ->propertyCondition('status', 1, '=')
      ->fieldCondition('commerce_stock', 'value', 'NULL', '!=');
    $products_result = $product_query
      ->execute();
    if (!empty($products_result)) {
      $products_result = reset($products_result);
      $products = entity_load('commerce_product', array_keys($products_result));

      // Key the list by SKU.
      // @todo: Is there a way to do this already in core Commerce?
      foreach ($products as $pid => $product) {
        $product_list[$product->sku] = $product;
      }
    }
  }
  return $product_list;
}