You are here

function commerce_registration_cancel_registrations in Commerce Registration 7

Action callback.

Cancels all registrations on a particular order.

Parameters

$order: The Commerce Order object to cancel all attached registrations.

Related topics

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

File

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

Code

function commerce_registration_cancel_registrations($order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    $type = $line_item_wrapper->commerce_product->type
      ->value();
    $id = $line_item_wrapper->commerce_product->product_id
      ->value();
    $can_register = registration_entity_registration_status(array(
      'id' => $id,
      'type' => 'commerce_product',
      'bundle' => $type,
    ));
    if ($can_register == 1) {
      $product = $line_item_wrapper->commerce_product;
      $quantity = (int) $line_item_wrapper->quantity
        ->value();
      for ($i = 0; $i < $quantity; $i++) {
        $entity = $order->data['register_entities']['prod-' . $product->sku
          ->value()][$i];

        // notify field api that the entity was deleted
        field_attach_delete('registration', $entity);
        entity_delete('registration', $entity->registration_id);
      }
    }
  }
}