You are here

function _commerce_webform_create_order_line_items in Commerce Webform 8

Same name and namespace in other branches
  1. 7.2 commerce_webform.rules.inc \_commerce_webform_create_order_line_items()
  2. 7 commerce_webform.rules.inc \_commerce_webform_create_order_line_items()

Helper function to create new line items from the webform submission. Used by webform creation and webform update rules.

Parameters

array $product_details: The product details as supplied by a call to commerce_webform_get_products_from_webform_submission().

int $order_id: The order to link the line items to (0 for current cart).

int $uid: The Drupal user uid who owns the order.

int $nid: The webform node nid.

int $sid: The webform submission sid

2 calls to _commerce_webform_create_order_line_items()
commerce_webform_order_create in ./commerce_webform.rules.inc
Rules action callback. Adds the product stored in a webform submission to the users cart.
commerce_webform_order_update in ./commerce_webform.rules.inc
Rules actions. Update an order from a commerce webform update.

File

./commerce_webform.rules.inc, line 181
Rules extras supplied by the commerce webform module.

Code

function _commerce_webform_create_order_line_items($product_details, $order_id, $uid, $nid, $sid) {
  if ($order_id) {

    // Setup the wrapper for the order.
    $order = commerce_order_load($order_id);
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  }
  foreach ($product_details as $product_detail) {
    $state = array(
      'commerce_webform' => array(
        'choose_quantity' => $product_detail->choose_quantity,
        'required' => $product_detail->required,
      ),
    );

    // Create a new line item.
    $line_item = commerce_product_line_item_new($product_detail->product, $product_detail->quantity, $order_id, $state, 'commerce_webform');

    // Save the line item to get its ID.
    commerce_line_item_save($line_item);

    // Add the sid to the line item.
    $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
    $line_item_wrapper->commerce_webform_sid
      ->set($sid);
    $line_item_wrapper->commerce_webform_nid
      ->set($nid);

    // Resave the line item with sid info.
    commerce_line_item_save($line_item);

    // Update the webform submission for this product field.
    $webform_submission_object = webform_get_submission($nid, $sid);
    commerce_webform_update_webform_submission_productfield($webform_submission_object, $order_id, $line_item->line_item_id, $product_detail->product->product_id, $product_detail->quantity, FALSE);

    // Allow modules to prepare this as necessary. This hook is defined by the
    // Product Pricing module.
    drupal_alter('commerce_product_calculate_sell_price_line_item', $line_item);

    // Process the unit price through Rules so it reflects the user's actual
    // purchase price.
    rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
    if ($order_id) {

      // Add the line item to the special order.
      $order_wrapper->commerce_line_items[] = $line_item;
    }
    else {

      // Add item to the shopping cart.
      commerce_cart_product_add($uid, $line_item, TRUE);
    }
  }
  if ($order_id) {

    // Save the order again to update its line item reference field.
    commerce_order_save($order);
  }
}