You are here

function commerce_registration_waitlist_to_cart in Commerce Registration 7

Action callback.

Moves the product on the waitlist line item to a normal product line item.

Parameters

$line_item: Commerce Line Item that we are moving to a normal product line item.

Related topics

1 string reference to 'commerce_registration_waitlist_to_cart'
commerce_registration_rules_action_info in ./commerce_registration.rules.inc
Implements hook_rules_action_info().

File

./commerce_registration.rules.inc, line 492
Commerce Registration rules file.

Code

function commerce_registration_waitlist_to_cart($line_item) {
  if (!isset($line_item)) {
    return;
  }
  $line_item->type = 'product';
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $order = commerce_order_load($line_item_wrapper->order_id
    ->value());
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

  // We search for an existing line item of the same product to possibly
  // combine with.
  $combine_line_item = NULL;
  foreach ($order_wrapper->commerce_line_items as $delta => $combine_line_item_wrapper) {

    // If this line item matches the product being added...
    if (empty($combine_line_item) && $combine_line_item_wrapper->type
      ->value() == 'product' && $combine_line_item_wrapper->commerce_product->product_id
      ->value() == $line_item_wrapper->commerce_product->product_id
      ->value()) {
      $combine_line_item = $combine_line_item_wrapper
        ->value();
    }
  }
  $new_line_item = clone $line_item;
  unset($new_line_item->line_item_id);
  if (!empty($combine_line_item)) {

    // Existing waitlist item, so we add it's quantity to our new line_item.
    $new_line_item->quantity += $combine_line_item->quantity;
  }

  // Save the new line_item and add it to our order.
  commerce_line_item_save($new_line_item);
  $order_wrapper->commerce_line_items[] = $new_line_item;
  if (!empty($combine_line_item)) {

    // Existing waitlist item, we delete it so we don't have two identical line
    // items.
    $order = commerce_cart_order_product_line_item_delete($order, $combine_line_item->line_item_id);
    commerce_order_save($order);
  }
  return $new_line_item;
}