diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 44902a5..fa4f4d3 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -926,7 +926,11 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : } if (isset($order['company'])) { - $data->setNewCompany($order['company']); + if (empty($crmOrder)) { + $crmOrder = $this->getCRMOrder($order['id'], 'id'); + } + + $data->setNewCompany($crmOrder['company']); } if ($data->feasible()) { diff --git a/src/include/components/class-wc-retailcrm-customer-switcher.php b/src/include/components/class-wc-retailcrm-customer-switcher.php index d88c76d..b483c61 100644 --- a/src/include/components/class-wc-retailcrm-customer-switcher.php +++ b/src/include/components/class-wc-retailcrm-customer-switcher.php @@ -41,6 +41,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface $newCustomer = $this->data->getNewCustomer(); $newContact = $this->data->getNewContact(); $newCompany = $this->data->getNewCompanyName(); + $companyAddress = $this->data->getCompanyAddress(); if (!empty($newCustomer)) { WC_Retailcrm_Logger::debug( @@ -76,6 +77,10 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface ); $this->processCompanyChange(); } + + if (!empty($companyAddress)) { + $this->processCompanyAddress(); + } } return $this; @@ -140,16 +145,31 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface } $address = self::arrayValue($newCustomer, 'address', array()); - $wcOrder->set_billing_state(self::arrayValue($address, 'region', '--')); - $wcOrder->set_billing_postcode(self::arrayValue($address, 'index', '--')); - $wcOrder->set_billing_country(self::arrayValue($address, 'country', '--')); - $wcOrder->set_billing_city(self::arrayValue($address, 'city', '--')); - $wcOrder->set_billing_address_1(self::arrayValue($address, 'text', '--')); + self::setBillingAddressToOrder($wcOrder, $address); $wcOrder->set_billing_phone(self::singleCustomerPhone($newCustomer)); $this->result = new WC_Retailcrm_Customer_Switcher_Result($wcCustomer, $wcOrder); } + /** + * Process company address. + * + * @throws \WC_Data_Exception + */ + protected function processCompanyAddress() + { + $wcOrder = $this->data->getWcOrder(); + $companyAddress = $this->data->getCompanyAddress(); + + if (!empty($companyAddress)) { + self::setBillingAddressToOrder($wcOrder, $companyAddress); + } + + if (empty($this->result)) { + $this->result = new WC_Retailcrm_Customer_Switcher_Result(null, $wcOrder); + } + } + /** * This will update company field in order and create result if it's not set (happens when only company was changed). * @@ -207,6 +227,23 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface return $this->data; } + /** + * Sets billing address properties in order + * + * @param \WC_Order $wcOrder + * @param array $address + * + * @throws \WC_Data_Exception + */ + private static function setBillingAddressToOrder($wcOrder, $address) + { + $wcOrder->set_billing_state(self::arrayValue($address, 'region', '--')); + $wcOrder->set_billing_postcode(self::arrayValue($address, 'index', '--')); + $wcOrder->set_billing_country(self::arrayValue($address, 'country', '--')); + $wcOrder->set_billing_city(self::arrayValue($address, 'city', '--')); + $wcOrder->set_billing_address_1(self::arrayValue($address, 'text', '--')); + } + /** * @param array|\ArrayObject|\ArrayAccess $arr * @param string $key diff --git a/src/include/models/class-wc-retailcrm-customer-switcher-state.php b/src/include/models/class-wc-retailcrm-customer-switcher-state.php index 765bf3c..7364b8e 100644 --- a/src/include/models/class-wc-retailcrm-customer-switcher-state.php +++ b/src/include/models/class-wc-retailcrm-customer-switcher-state.php @@ -18,6 +18,9 @@ class WC_Retailcrm_Customer_Switcher_State /** @var string $newCompanyName */ private $newCompanyName; + /** @var array $companyAddress */ + private $companyAddress; + /** * @return \WC_Order */ @@ -94,6 +97,25 @@ class WC_Retailcrm_Customer_Switcher_State return $this; } + /** + * @return array + */ + public function getCompanyAddress() + { + return $this->companyAddress; + } + + /** + * @param array $companyAddress + * + * @return WC_Retailcrm_Customer_Switcher_State + */ + public function setCompanyAddress($companyAddress) + { + $this->companyAddress = $companyAddress; + return $this; + } + /** * @param array $newCompany * @@ -105,6 +127,10 @@ class WC_Retailcrm_Customer_Switcher_State $this->setNewCompanyName($newCompany['name']); } + if (isset($newCompany['address']) && !empty($newCompany['address'])) { + $this->setCompanyAddress($newCompany['address']); + } + return $this; }