function commerce_product_line_item_populate in Commerce Core 7
Populates an existing product line item with the product and quantity data.
Parameters
$line_item: The fully loaded line item object, populated by reference.
$product: The fully loaded product referenced by the line item.
3 calls to commerce_product_line_item_populate()
- commerce_cart_order_refresh in modules/
cart/ commerce_cart.module - Refreshes the contents of a shopping cart by finding the most current prices for any product line items on the order.
- commerce_product_line_item_add_form_submit in modules/
product_reference/ commerce_product_reference.module - Adds the selected product information to a line item added via a line item manager widget.
- commerce_product_line_item_new in modules/
product_reference/ commerce_product_reference.module - Creates a new product line item populated with the proper product values.
File
- modules/
product_reference/ commerce_product_reference.module, line 1420 - Defines a field type for referencing products from other entities.
Code
function commerce_product_line_item_populate($line_item, $product) {
// Set the label to be the product SKU.
$line_item->line_item_label = $product->sku;
// Wrap the line item and product to easily set field information.
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
// 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 if the product has a value in
// its commerce_price field.
$line_item->commerce_unit_price = $product->commerce_price;
if (!is_null($line_item_wrapper->commerce_unit_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);
}
}
}