From a5f6a8474100bfd69c6d72f8422c8bda7a4fa529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB?= Date: Wed, 8 Jul 2020 14:22:18 +0300 Subject: [PATCH] address fixes --- .../class-wc-retailcrm-abstracts-address.php | 26 +++++++++++++++-- src/include/class-wc-retailcrm-customers.php | 4 +-- ...c-retailcrm-customer-corporate-address.php | 28 ++++++++----------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-address.php b/src/include/abstracts/class-wc-retailcrm-abstracts-address.php index 699a115..aae346e 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-address.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-address.php @@ -20,6 +20,9 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat /** @var bool $fallback_to_billing */ protected $fallback_to_billing = false; + /** @var bool $fallback_to_shipping */ + protected $fallback_to_shipping = false; + /** @var array $data */ protected $data = array( 'index' => '', @@ -54,6 +57,17 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat return $this; } + /** + * @param bool $fallback_to_shipping + * + * @return WC_Retailcrm_Abstracts_Address + */ + public function setFallbackToShipping($fallback_to_shipping) + { + $this->fallback_to_shipping = $fallback_to_shipping; + return $this; + } + /** * Sets woocommerce address type to work with * @@ -78,9 +92,15 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat { $orderAddress = $order->get_address($this->address_type); - return (empty($orderAddress) && $this->fallback_to_billing) - ? $order->get_address(self::ADDRESS_TYPE_BILLING) - : $order->get_address($this->address_type); + if (empty($orderAddress) && $this->address_type === self::ADDRESS_TYPE_BILLING && $this->fallback_to_shipping) { + $orderAddress = $order->get_address(self::ADDRESS_TYPE_SHIPPING); + } + + if (empty($orderAddress) && $this->address_type === self::ADDRESS_TYPE_SHIPPING && $this->fallback_to_billing) { + $orderAddress = $order->get_address(self::ADDRESS_TYPE_BILLING); + } + + return $orderAddress; } /** diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index ef11dd1..981b921 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -257,10 +257,10 @@ if (!class_exists('WC_Retailcrm_Customers')) : $found = false; $builder = new WC_Retailcrm_Customer_Corporate_Address(); $newAddress = $builder - ->setFallbackToBilling(true) + ->setFallbackToShipping(true) ->setIsMain(false) ->setExplicitIsMain(false) - ->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_SHIPPING) + ->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING) ->build($customer, $order) ->get_data(); $addresses = $this->retailcrm->customersCorporateAddresses( diff --git a/src/include/customer/class-wc-retailcrm-customer-corporate-address.php b/src/include/customer/class-wc-retailcrm-customer-corporate-address.php index 6bd44de..088c432 100644 --- a/src/include/customer/class-wc-retailcrm-customer-corporate-address.php +++ b/src/include/customer/class-wc-retailcrm-customer-corporate-address.php @@ -17,12 +17,6 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add /** @var string $filter_name */ protected $filter_name = 'customer_address'; - /** @var string $address_type */ - protected $address_type = 'shipping'; - - /** @var bool $fallback_to_billing */ - protected $fallback_to_billing = false; - /** @var bool $isMain */ protected $isMain = true; @@ -43,13 +37,13 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add } /** - * @param bool $fallback_to_billing + * @param bool $fallback_to_shipping * * @return WC_Retailcrm_Customer_Corporate_Address */ - public function setFallbackToBilling($fallback_to_billing) + public function setFallbackToShipping($fallback_to_shipping) { - $this->fallback_to_billing = $fallback_to_billing; + $this->fallback_to_shipping = $fallback_to_shipping; return $this; } @@ -93,16 +87,18 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add 'name' => $address['company'], 'text' => $this->joinAddresses($address['address_1'], $address['address_2']) ); - } else { - if (WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_SHIPPING == $this->address_type) { - $data = $this->getCustomerBillingAddress($customer); + } else if (self::ADDRESS_TYPE_SHIPPING === $this->address_type) { + $data = $this->getCustomerShippingAddress($customer); - if (empty($address) && $this->fallback_to_billing) { - $data = $this->getCustomerShippingAddress($customer); - } - } else { + if (empty($address) && $this->fallback_to_billing) { $data = $this->getCustomerBillingAddress($customer); } + } elseif (self::ADDRESS_TYPE_BILLING === $this->address_type) { + $data = $this->getCustomerBillingAddress($customer); + + if (empty($address) && $this->fallback_to_shipping) { + $data = $this->getCustomerShippingAddress($customer); + } } if ($this->isMain) {