I had the requirement to check the equality of addresses in customer orders (billing / shipping) and wrote two helper functions for that.
Perhaps you'll run into the same demand one day. Then please leave a comment here and in the original issue I posted on Drupal.org to document your demand for such a solution: See https://www.drupal.org/node/2680997
Thank you!
My code (without any warranties):
/**
* Compares the order shipping address (commerce_customer_shipping) to the order billing address (commerce_customer_billing) and
* returns true if the shipping address differs from the billing address. If both are equal (doesn't have to mean same ID but same address data) returns false. *
*
* @param stdClass|int $commerce_order The commerce order object or ID.
* @return boolean
*/
function commerce_order_shipping_shipping_address_differs($commerce_order) {
$commerce_order_wrapper = entity_metadata_wrapper('commerce_order', $commerce_order);
$commerce_customer_billing_profile = $commerce_order_wrapper->commerce_customer_billing->value();
$commerce_customer_shipping_profile = $commerce_order_wrapper->commerce_customer_shipping->value();
return !commerce_customer_profile_equals($commerce_customer_billing_profile, $commerce_customer_shipping_profile);
}
/**
* Compares two customer profiles and returns true if they are the same (id equals) or both have exactly
* the same address data. Otherwise returns false.
*
* @param stdClass $commerce_customer_profile_a
* @param stdClass $commerce_customer_profile_b
* @return boolean
*/
function commerce_customer_profile_equals($commerce_customer_profile_a, $commerce_customer_profile_b) {
if (!empty($commerce_customer_profile_a->profile_id) && $commerce_customer_profile_a->profile_id == $commerce_customer_profile_b->profile_id) {
// The two profiles are the same.
return true;
}
if (!empty($commerce_customer_profile_a->commerce_customer_address) && !empty($commerce_customer_profile_b->commerce_customer_address)) {
if ($commerce_customer_profile_a->commerce_customer_address == $commerce_customer_profile_b->commerce_customer_address) {
return true;
}
}
return false;
}
?>