You are here

function commerce_registration_update_line_item_registrations in Commerce Registration 7.3

Delete any 'unused' registrations on all line items on the order. An 'unused' registration is any registration data that doesn't belong to a specific product quantity, ie: if there are 5 registrations, but the line item only has a quantity of 4. This would be a remnant of changing the quantity after already filling out registration information. This will free up any registration space for the associated event.

1 call to commerce_registration_update_line_item_registrations()
commerce_registration_commerce_checkout_complete in ./commerce_registration.module
Implements hook_commerce_checkout_complete().

File

./commerce_registration.module, line 624
Commerce Registration module code.

Code

function commerce_registration_update_line_item_registrations($line_item_wrapper) {
  if (!in_array($line_item_wrapper->type
    ->value(), commerce_product_line_item_types())) {
    return;
  }
  $product_id = $line_item_wrapper->commerce_product->product_id
    ->value();
  if (commerce_registration_product_has_registration_field($product_id)) {
    $quantity = $line_item_wrapper->quantity
      ->value();
    $registrations = $line_item_wrapper->registrations
      ->value();
    if (count($registrations) > $quantity) {

      // Only need to do this if there are more registration quantities than
      // the line item quantity.
      $registration_new = array_slice($registrations, 0, $quantity);
      $registration_trim = array_slice($registrations, $quantity);
      foreach ($registration_trim as $registration) {
        registration_delete_multiple(array(
          $registration->registration_id,
        ));
      }
      $line_item_wrapper->registrations = $registration_new;
      $line_item_wrapper
        ->save();
    }
  }
}