You are here

function commerce_order_commerce_customer_profile_can_delete in Commerce Core 7

Implements hook_commerce_customer_profile_can_delete().

2 calls to commerce_order_commerce_customer_profile_can_delete()
commerce_order_commerce_customer_profile_presave in modules/order/commerce_order.module
Implements hook_commerce_customer_profile_presave().
commerce_order_form_commerce_customer_customer_profile_form_alter in modules/order/commerce_order.module
Implements hook_form_FORM_ID_alter().

File

modules/order/commerce_order.module, line 464
Defines the core Commerce order entity and API functions to manage orders and interact with them.

Code

function commerce_order_commerce_customer_profile_can_delete($profile) {

  // Look for any non-cart order with a reference field targeting the profile.
  foreach (commerce_info_fields('commerce_customer_profile_reference') as $field_name => $field) {

    // Use EntityFieldQuery to look for orders referencing this customer profile
    // and do not allow the delete to occur if one exists.
    $query = new EntityFieldQuery();
    $query
      ->addTag('commerce_order_commerce_customer_profile_can_delete')
      ->entityCondition('entity_type', 'commerce_order', '=')
      ->fieldCondition($field_name, 'profile_id', $profile->profile_id, '=')
      ->count();

    // Add a condition on the order status if there are cart order statuses.
    $statuses = array_keys(commerce_order_statuses(array(
      'cart' => TRUE,
    )));
    if (!empty($statuses)) {
      $query
        ->propertyCondition('status', $statuses, 'NOT IN');
    }

    // If the profile includes an order context property, we know this was added
    // by the Order module as an order ID to skip in the deletion query.
    if (!empty($profile->entity_context['entity_id']) && $profile->entity_context['entity_type'] == 'commerce_order') {
      $query
        ->propertyCondition('order_id', $profile->entity_context['entity_id'], '!=');
    }
    if ($query
      ->execute() > 0) {
      return FALSE;
    }
  }
  return TRUE;
}