From 286cb2d1c800c159b95d059c0df6c9ea6bb6d9f7 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 17 Nov 2016 15:05:40 +0000 Subject: [PATCH] get_address_prop for customers --- includes/class-wc-customer.php | 260 +++++++++--------- .../class-wc-order-data-store-cpt.php | 4 + 2 files changed, 135 insertions(+), 129 deletions(-) diff --git a/includes/class-wc-customer.php b/includes/class-wc-customer.php index b9e29b5b41f..d0383f55b18 100644 --- a/includes/class-wc-customer.php +++ b/includes/class-wc-customer.php @@ -470,243 +470,245 @@ class WC_Customer extends WC_Legacy_Customer { } /** - * Gets customer billing first name. + * Gets a prop for a getter method. + * + * @since 2.7.0 + * @param string $prop Name of prop to get. + * @param string $address billing or shipping. + * @param string $context What the value is for. Valid values are view and edit. + * @return mixed + */ + protected function get_address_prop( $prop, $address = 'billing', $context = 'view' ) { + $value = null; + + if ( array_key_exists( $prop, $this->data[ $address ] ) ) { + $value = isset( $this->changes[ $address ][ $prop ] ) ? $this->changes[ $address ][ $prop ] : $this->data[ $address ][ $prop ]; + + if ( 'view' === $context ) { + $value = apply_filters( $this->get_hook_prefix() . $address . '_' . $prop, $value, $this ); + } + } + return $value; + } + + /** + * Get billing_first_name. * * @param string $context * @return string */ public function get_billing_first_name( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['first_name']; + return $this->get_address_prop( 'first_name', 'billing', $context ); } /** - * Gets customer billing last name. + * Get billing_last_name. * * @param string $context * @return string */ public function get_billing_last_name( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['last_name']; + return $this->get_address_prop( 'last_name', 'billing', $context ); } /** - * Gets customer billing company. + * Get billing_company. * * @param string $context * @return string */ public function get_billing_company( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['company']; + return $this->get_address_prop( 'company', 'billing', $context ); } /** - * Gets billing phone. - * - * @param string $context - * @return string - */ - public function get_billing_phone( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['phone']; - } - - /** - * Gets billing email. - * - * @param string $context - * @return string - */ - public function get_billing_email( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['email']; - } - - /** - * Gets customer postcode. - * - * @param string $context - * @return string - */ - public function get_billing_postcode( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return 'view' === $context ? wc_format_postcode( $billing['postcode'], $this->get_billing_country() ) : $billing['postcode']; - } - - /** - * Get customer city. - * - * @param string $context - * @return string - */ - public function get_billing_city( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['city']; - } - - /** - * Get customer address. + * Get billing_address_1. * * @param string $context * @return string */ public function get_billing_address( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['address_1']; + return $this->get_billing_address_1( $context ); } /** - * Get customer address. + * Get billing_address_1. * * @param string $context * @return string */ public function get_billing_address_1( $context = 'view' ) { - return $this->get_billing_address( $context ); + return $this->get_address_prop( 'address_1', 'billing', $context ); } /** - * Get customer's second address. + * Get billing_address_2. * * @param string $context - * @return string + * @return string $value */ public function get_billing_address_2( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['address_2']; + return $this->get_address_prop( 'address_2', 'billing', $context ); } /** - * Get customer state. + * Get billing_city. + * + * @param string $context + * @return string $value + */ + public function get_billing_city( $context = 'view' ) { + return $this->get_address_prop( 'city', 'billing', $context ); + } + + /** + * Get billing_state. * * @param string $context * @return string */ public function get_billing_state( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['state']; + return $this->get_address_prop( 'state', 'billing', $context ); } /** - * Get customer country. + * Get billing_postcode. + * + * @param string $context + * @return string + */ + public function get_billing_postcode( $context = 'view' ) { + return $this->get_address_prop( 'postcode', 'billing', $context ); + } + + /** + * Get billing_country. * * @param string $context * @return string */ public function get_billing_country( $context = 'view' ) { - $billing = $this->get_prop( 'billing', $context ); - return $billing['country']; + return $this->get_address_prop( 'country', 'billing', $context ); } /** - * Gets customer shipping first name. + * Get billing_email. + * + * @param string $context + * @return string + */ + public function get_billing_email( $context = 'view' ) { + return $this->get_address_prop( 'email', 'billing', $context ); + } + + /** + * Get billing_phone. + * + * @param string $context + * @return string + */ + public function get_billing_phone( $context = 'view' ) { + return $this->get_address_prop( 'phone', 'billing', $context ); + } + + /** + * Get shipping_first_name. * * @param string $context * @return string */ public function get_shipping_first_name( $context = 'view' ) { - $shipping = $this->get_prop( 'shipping', $context ); - return $shipping['first_name']; + return $this->get_address_prop( 'first_name', 'shipping', $context ); } /** - * Gets customer shipping last name. + * Get shipping_last_name. * * @param string $context * @return string */ public function get_shipping_last_name( $context = 'view' ) { - $shipping = $this->get_prop( 'shipping', $context ); - return $shipping['last_name']; + return $this->get_address_prop( 'last_name', 'shipping', $context ); } /** - * Gets customer shipping company. + * Get shipping_company. * * @param string $context * @return string */ public function get_shipping_company( $context = 'view' ) { - $shipping = $this->get_prop( 'shipping', $context ); - return $shipping['company']; + return $this->get_address_prop( 'company', 'shipping', $context ); } /** - * Get customer's shipping state. - * - * @param string $context - * @return string - */ - public function get_shipping_state( $context = 'view' ) { - $shipping = $this->get_prop( 'shipping', $context ); - return $shipping['state']; - } - - /** - * Get customer's shipping country. - * - * @param string $context - * @return string - */ - public function get_shipping_country( $context = 'view' ) { - $shipping = $this->get_prop( 'shipping', $context ); - return $shipping['country']; - } - - /** - * Get customer's shipping postcode. - * - * @param string $context - * @return string - */ - public function get_shipping_postcode( $context = 'view' ) { - $shipping = $this->get_prop( 'shipping', $context ); - return 'view' === $context ? wc_format_postcode( $shipping['postcode'], $this->get_shipping_country() ) : $shipping['postcode']; - } - - /** - * Get customer's shipping city. - * - * @param string $context - * @return string - */ - public function get_shipping_city( $context = 'view' ) { - $shipping = $this->get_prop( 'shipping', $context ); - return $shipping['city']; - } - - /** - * Get customer's shipping address. + * Get shipping_address_1. * * @param string $context * @return string */ public function get_shipping_address( $context = 'view' ) { - $shipping = $this->get_prop( 'shipping', $context ); - return $shipping['address_1']; + return $this->get_shipping_address_1( $context ); } /** - * Get customer address. + * Get shipping_address_1. * * @param string $context * @return string */ public function get_shipping_address_1( $context = 'view' ) { - return $this->get_shipping_address( $context ); + return $this->get_address_prop( 'address_1', 'shipping', $context ); } /** - * Get customer's second shipping address. + * Get shipping_address_2. * * @param string $context * @return string */ public function get_shipping_address_2( $context = 'view' ) { - $shipping = $this->get_prop( 'shipping', $context ); - return $shipping['address_2']; + return $this->get_address_prop( 'address_2', 'shipping', $context ); + } + + /** + * Get shipping_city. + * + * @param string $context + * @return string + */ + public function get_shipping_city( $context = 'view' ) { + return $this->get_address_prop( 'city', 'shipping', $context ); + } + + /** + * Get shipping_state. + * + * @param string $context + * @return string + */ + public function get_shipping_state( $context = 'view' ) { + return $this->get_address_prop( 'state', 'shipping', $context ); + } + + /** + * Get shipping_postcode. + * + * @param string $context + * @return string + */ + public function get_shipping_postcode( $context = 'view' ) { + return $this->get_address_prop( 'postcode', 'shipping', $context ); + } + + /** + * Get shipping_country. + * + * @param string $context + * @return string + */ + public function get_shipping_country( $context = 'view' ) { + return $this->get_address_prop( 'country', 'shipping', $context ); } /** diff --git a/includes/data-stores/class-wc-order-data-store-cpt.php b/includes/data-stores/class-wc-order-data-store-cpt.php index e9fe01c6b89..ac9b005c000 100644 --- a/includes/data-stores/class-wc-order-data-store-cpt.php +++ b/includes/data-stores/class-wc-order-data-store-cpt.php @@ -114,6 +114,10 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement '_cart_hash' => 'cart_hash', ); foreach ( $meta_key_to_props as $meta_key => $prop ) { + + + // ADDRESSES @todo + if ( ! in_array( $prop, $changed_props ) ) { continue; }