Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Based in #2680915: Add source information to copied commerce_customer_profile I decided to write two helper functions to determine if two addresses (specifically the shipping and the billing adress within an order) are equal.
Because I think this might be helpful for other cases I'd like to present these functions to you. Feel free to add them to the project or use them for custom modules:
/**
* 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;
}
The naming currently assumes that these will be added to the commerce_order / commerce_customer modules one day.
Comments
Comment #2
Anybody