You are here

function commerce_product_bundle_line_item_new in Commerce Product Bundle 7

Same name and namespace in other branches
  1. 7.2 commerce_product_bundle.module \commerce_product_bundle_line_item_new()

Creates a new product line item populated with the proper product values.

Parameters

obj $product: The subproduct.

obj $parent_line_item: The line item object of the bundle product.

int $quantity: The quantity of the subproduct.

int $order_id: The id of the order to which the product belongs to.

array $data: A data array to set on the new line item. The following information in the data array may be used on line item creation:

  • $data['context']['display_path']: if present will be used to set the line item's display_path field value.

Return value

obj Line item object with default values.

3 calls to commerce_product_bundle_line_item_new()
commerce_product_bundle_add_to_cart in ./commerce_product_bundle.module
Adds the specified product to a customer's shopping cart.
commerce_product_bundle_attribute_product_field_alter in ./commerce_product_bundle.module
Implements hook_attribute_product_field_alter().
commerce_product_bundle_update_cart in ./commerce_product_bundle.module
Updates the specified product in a customer's shopping cart.

File

./commerce_product_bundle.module, line 1204
Allows the bundling of products in Drupal Commerce.

Code

function commerce_product_bundle_line_item_new($product, $parent_line_item, $quantity = 1, $order_id = 0, $data = array()) {

  // Create the new line item.
  $line_item = entity_create('commerce_line_item', array(
    'type' => 'bundle',
    'quantity' => $quantity,
    'data' => $data,
  ));

  // Set the label to be the product SKU.
  $line_item->line_item_label = $product->sku;

  // Set the incoming parents line item's order_id.
  $line_item->order_id = $parent_line_item->order_id;

  // Wrap the line item and product to easily set field information.
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $product_wrapper = entity_metadata_wrapper('commerce_product', $product);

  // Add the product reference value to the line item for the right language.
  $line_item_wrapper->commerce_product = $product->product_id;

  // Add the display URI if specified.
  if (!empty($line_item->data['context']['display_path'])) {
    $line_item_wrapper->commerce_display_path = $line_item->data['context']['display_path'];
  }
  else {
    $line_item_wrapper->commerce_display_path = '';
  }

  // Set the unit price on the line item object.
  $line_item_wrapper->commerce_unit_price = $product_wrapper->commerce_price
    ->value();

  // Add the base price to the components array.
  if (!commerce_price_component_load($line_item_wrapper->commerce_unit_price
    ->value(), 'base_price')) {
    $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($line_item_wrapper->commerce_unit_price
      ->value(), 'base_price', $line_item_wrapper->commerce_unit_price
      ->value(), TRUE);
  }

  // Add the parent line item.
  $line_item_wrapper->commerce_parent_line_item = $parent_line_item->line_item_id;

  // Return the line item.
  return $line_item;
}