You are here

function commerce_cart_add_to_cart_form_product_ids in Commerce Core 7

Returns an array of product IDs used for building an Add to Cart form from the context information in a line item's data array.

Parameters

$line_item: The line item whose data array includes a context array used for building an Add to Cart form.

Return value

The array of product IDs extracted from the line item.

See also

commerce_cart_add_to_cart_form()

2 calls to commerce_cart_add_to_cart_form_product_ids()
commerce_cart_add_to_cart_form in modules/cart/commerce_cart.module
Builds an Add to Cart form for a set of products.
commerce_cart_handler_field_edit_attributes::views_form in modules/cart/includes/views/handlers/commerce_cart_handler_field_edit_attributes.inc
Returns the form which replaces the placeholder from render().

File

modules/cart/commerce_cart.module, line 2873
Implements the shopping cart system and add to cart features.

Code

function commerce_cart_add_to_cart_form_product_ids($line_item) {
  $product_ids = array();
  if (empty($line_item->data['context']) || empty($line_item->data['context']['product_ids']) || $line_item->data['context']['product_ids'] == 'entity' && empty($line_item->data['context']['entity'])) {
    return $product_ids;
  }

  // If the product IDs setting tells us to use entity values...
  if ($line_item->data['context']['product_ids'] == 'entity' && is_array($line_item->data['context']['entity'])) {
    $entity_data = $line_item->data['context']['entity'];

    // Load the specified entity.
    $entity = entity_load_single($entity_data['entity_type'], $entity_data['entity_id']);

    // Extract the product IDs from the specified product reference field.
    if (!empty($entity->{$entity_data['product_reference_field_name']})) {
      $product_ids = entity_metadata_wrapper($entity_data['entity_type'], $entity)->{$entity_data['product_reference_field_name']}
        ->raw();
    }
  }
  elseif (is_array($line_item->data['context']['product_ids'])) {
    $product_ids = $line_item->data['context']['product_ids'];
  }
  return $product_ids;
}