function commerce_product_line_item_new in Commerce Core 7
Creates a new product line item populated with the proper product values.
Parameters
$product: The fully loaded product referenced by the line item.
$quantity: The quantity to set for the product.
$order_id: The ID of the order the line item belongs to (if available).
$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.
$type: The type of product line item to create. Must be a product line item as defined in the line item type info array, and the line item type must include the expected product related fields. Defaults to the base product line item type defined by the Product Reference module.
Return value
The fully loaded line item populated with the product data as specified.
7 calls to commerce_product_line_item_new()
- CommerceBaseTestCase::createDummyOrder in tests/
commerce_base.test - Create a dummy order in a given status.
- commerce_cart_add_to_cart_form_submit in modules/
cart/ commerce_cart.module - Form submit handler: add the selected product to the cart.
- commerce_cart_field_formatter_view in modules/
cart/ commerce_cart.module - Implements hook_field_formatter_view().
- commerce_cart_handler_field_add_to_cart_form::render in modules/
cart/ includes/ views/ handlers/ commerce_cart_handler_field_add_to_cart_form.inc - Render the field.
- commerce_cart_product_add_by_id in modules/
cart/ commerce_cart.module - Adds the specified product to a customer's shopping cart by product ID.
File
- modules/
product_reference/ commerce_product_reference.module, line 1391 - Defines a field type for referencing products from other entities.
Code
function commerce_product_line_item_new($product, $quantity = 1, $order_id = 0, $data = array(), $type = 'product') {
// Ensure a default product line item type.
if (empty($type)) {
$type = 'product';
}
// Create the new line item.
$line_item = entity_create('commerce_line_item', array(
'type' => $type,
'order_id' => $order_id,
'quantity' => $quantity,
'data' => $data,
));
// Populate it with the product information.
commerce_product_line_item_populate($line_item, $product);
// Return the line item.
return $line_item;
}