Drupal 7 Commerce: Compare customer profile addresses / check address equality (shipping / billing)

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):

  1. /**
  2.  * Compares the order shipping address (commerce_customer_shipping) to the order billing address (commerce_customer_billing) and
  3.  * 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. *
  4.  *
  5.  * @param stdClass|int $commerce_order The commerce order object or ID.
  6.  * @return boolean
  7.  */
  8. function commerce_order_shipping_shipping_address_differs($commerce_order) {
  9.   $commerce_order_wrapper = entity_metadata_wrapper('commerce_order', $commerce_order);
  10.   $commerce_customer_billing_profile = $commerce_order_wrapper->commerce_customer_billing->value();
  11.   $commerce_customer_shipping_profile = $commerce_order_wrapper->commerce_customer_shipping->value();
  12.   return !commerce_customer_profile_equals($commerce_customer_billing_profile, $commerce_customer_shipping_profile);
  13. }
  14.  
  15. /**
  16.  * Compares two customer profiles and returns true if they are the same (id equals) or both have exactly
  17.  * the same address data. Otherwise returns false.
  18.  *
  19.  * @param stdClass $commerce_customer_profile_a
  20.  * @param stdClass $commerce_customer_profile_b
  21.  * @return boolean
  22.  */
  23. function commerce_customer_profile_equals($commerce_customer_profile_a, $commerce_customer_profile_b) {
  24.   if (!empty($commerce_customer_profile_a->profile_id) && $commerce_customer_profile_a->profile_id == $commerce_customer_profile_b->profile_id) {
  25.     // The two profiles are the same.
  26.     return true;
  27.   }
  28.  
  29.   if (!empty($commerce_customer_profile_a->commerce_customer_address) && !empty($commerce_customer_profile_b->commerce_customer_address)) {
  30.     if ($commerce_customer_profile_a->commerce_customer_address == $commerce_customer_profile_b->commerce_customer_address) {
  31.       return true;
  32.     }
  33.   }
  34.   return false;
  35. }

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.

Weitere Informationen über Formatierungsoptionen