You are here

class CommerceSearchApiAlterProductStatus in Commerce Search API 7

Data alteration callback that filters out products that aren't published

Hierarchy

Expanded class hierarchy of CommerceSearchApiAlterProductStatus

1 string reference to 'CommerceSearchApiAlterProductStatus'
commerce_search_api_search_api_alter_callback_info in ./commerce_search_api.module
Implements hook_search_api_alter_callback_info().

File

includes/callback_product_status.inc, line 12
Products data alteration callback.

View source
class CommerceSearchApiAlterProductStatus extends SearchApiAbstractAlterCallback {

  /**
   * Check whether this data-alter callback is applicable for a certain index.
   *
   * @param SearchApiIndex $index
   *   The SearchApiIndex this data alteration callback resides on.
   *
   * @return bool
   *   A boolean whether or not this index is supported.
   */
  public function supportsIndex(SearchApiIndex $index) {
    $bundles = commerce_product_reference_node_types();
    return $index
      ->getEntityType() == 'node' && !empty($bundles);
  }

  /**
   * Exclude products that aren't published.
   */
  public function alterItems(array &$items) {
    if ($bundles = commerce_product_reference_node_types()) {
      $product_reference_fields = commerce_info_fields('commerce_product_reference', 'node');
      foreach ($items as $id => &$item) {
        if (!isset($bundles[$item->type])) {
          continue;
        }
        foreach ($product_reference_fields as $field_name => $field) {
          if (empty($item->{$field_name})) {
            continue;
          }
          $node_wrapper = entity_metadata_wrapper('node', $item);
          $nb_products = $field['cardinality'] == 1 ? 1 : $node_wrapper->{$field_name}
            ->count();
          $products_removed = 0;
          if ($field['cardinality'] == 1) {
            if ($node_wrapper->{$field_name}->status
              ->raw() == 0) {
              $products_removed = 1;
            }
          }
          else {
            foreach ($node_wrapper->{$field_name} as $delta => $product_wrapper) {
              if (!$product_wrapper
                ->value() || $product_wrapper->status
                ->raw() == 0) {
                $products_removed++;
                if ($products_removed != $nb_products) {
                  $node_wrapper->{$field_name}
                    ->offsetUnset($delta);
                }
              }
            }
          }

          // Remove the product display if all the associated products are
          // unpublished.
          if ($products_removed == $nb_products) {
            unset($items[$id]);
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceSearchApiAlterProductStatus::alterItems public function Exclude products that aren't published. Overrides SearchApiAlterCallbackInterface::alterItems
CommerceSearchApiAlterProductStatus::supportsIndex public function Check whether this data-alter callback is applicable for a certain index. Overrides SearchApiAbstractAlterCallback::supportsIndex
SearchApiAbstractAlterCallback::$index protected property The index whose items will be altered.
SearchApiAbstractAlterCallback::$options protected property The configuration options for this callback, if it has any.
SearchApiAbstractAlterCallback::configurationForm public function Implements SearchApiAlterCallbackInterface::configurationForm(). Overrides SearchApiAlterCallbackInterface::configurationForm 6
SearchApiAbstractAlterCallback::configurationFormSubmit public function Implements SearchApiAlterCallbackInterface::configurationFormSubmit(). Overrides SearchApiAlterCallbackInterface::configurationFormSubmit 4
SearchApiAbstractAlterCallback::configurationFormValidate public function Implements SearchApiAlterCallbackInterface::configurationFormValidate(). Overrides SearchApiAlterCallbackInterface::configurationFormValidate 1
SearchApiAbstractAlterCallback::isMultiEntityIndex protected function Determines whether the given index contains multiple types of entities.
SearchApiAbstractAlterCallback::propertyInfo public function Implements SearchApiAlterCallbackInterface::propertyInfo(). Overrides SearchApiAlterCallbackInterface::propertyInfo 6
SearchApiAbstractAlterCallback::__construct public function Implements SearchApiAlterCallbackInterface::__construct(). Overrides SearchApiAlterCallbackInterface::__construct 1