function commerce_registration_move_to_waitlist in Commerce Registration 7
Action callback.
Creates a new waitlist line item from an existing line item.
Combines identical products on to a single line item.
Parameters
$line_item: Commerce Line Item that we are moving to a waitlist.
Related topics
1 string reference to 'commerce_registration_move_to_waitlist'
File
- ./
commerce_registration.rules.inc, line 442 - Commerce Registration rules file.
Code
function commerce_registration_move_to_waitlist($line_item) {
if (!isset($line_item)) {
return;
}
$line_item->type = 'waitlist';
$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() == 'waitlist' && $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;
$new_line_item->type = 'waitlist';
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;
}