The documented hook to validate if a address should be deleted while modifying a user's address book doesn't validate correctly.

function hook_commerce_customer_profile_can_delete($profile) {
  // The default customer profile can not be deleted 
  if (example_profile_isdefault($profile) == TRUE) {
      return FALSE;
  }
}

function example_profile_isdefault($profileObj) {
  $result = db_select('commerce_addressbook_defaults', 'cad')
                ->fields('cad')
                ->condition('uid', $profileObj->uid, '=')
                ->condition('type', $profileObj->type, '=')
                ->condition('profile_id', $profileObj->profile_id, '=')
                ->execute();

  if ($result->rowCount() > 0) {
      return TRUE;
  } else {
      return FALSE;
  }
}

I return false but the address is deleting anyway.

What am I doing wrong?

Call stack:
- commerce_addressbook
- commerce_customer
- mymodule

Also why is commerce_customer_profile_save() being called even though I'm deleting the profile/address?
I assume this is to make sure past orders don't get orphaned?

Comments

vasike’s picture

Category: bug » support
Status: Active » Needs review

i tried your code and you're doing right.
the Commerce Addressbook disables your default profiles, that's why it's calling the commerce_customer_profile_save() function.
check the comments for the submit handler for addresses deletion

/**
 * Submit handler for commerce_addressbook_profile_options_delete_form.
 *
 * Deletes the customer profile if possible (when it's not referenced by any
 * orders), disables it otherwise.
 */

You could see the disabled profiles at Customer profiles page (admin/commerce/customer-profiles).

bojanz’s picture

Status: Needs review » Fixed

Confirming #1.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.