You are here

function commerce_product_bundle_attribute_product_field_alter in Commerce Product Bundle 7

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

Implements hook_attribute_product_field_alter().

File

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

Code

function commerce_product_bundle_attribute_product_field_alter(&$element, $product, $product_field_name, $form, $form_state) {
  if ($product_field_name == 'commerce_price') {

    // First create a pseudo product line item that we will pass to Rules.
    $line_item = commerce_product_line_item_new($product);
    $line_item->line_item_id = 0;
    $subproducts = array();

    // Add sub products to the cart:
    foreach ($form_state['values']['bundle'] as $id => $item_values) {
      $subproduct = $form_state['bundle'][$id]['default_product'];
      $sub_line_item = commerce_product_bundle_line_item_new($subproduct, $line_item, $item_values['quantity']);
      $sub_line_items[] = $sub_line_item;
    }
    $line_item->data['sub_line_items'] = $sub_line_items;

    // Pass the line item to Rules.
    rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
    $price = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_unit_price
      ->value();
    $element[0]['#markup'] = commerce_currency_format($price['amount'], $element['#items'][0]['currency_code'], $product);
  }
}