From 678c2d0820782dc2d5cee491821cab92b5112428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB?= Date: Tue, 16 Jun 2020 15:05:27 +0300 Subject: [PATCH] fixes for changed customer sync & correctly load created customer --- src/include/class-wc-retailcrm-base.php | 35 ++++++++++++++++--- src/include/class-wc-retailcrm-customers.php | 26 ++++++++++++++ .../class-wc-retailcrm-customer-switcher.php | 33 ++++++++++++++--- 3 files changed, 86 insertions(+), 8 deletions(-) diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index cdd6518..f6c6d84 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -96,6 +96,7 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('admin_print_footer_scripts', array($this, 'ajax_selected_order'), 99); add_action('woocommerce_created_customer', array($this, 'create_customer'), 10, 1); add_action('woocommerce_update_customer', array($this, 'update_customer'), 10, 1); + add_action('user_register', array($this, 'create_customer'), 10, 2); add_action('profile_update', array($this, 'update_customer'), 10, 2); add_action('wp_print_scripts', array($this, 'initialize_analytics'), 98); add_action('wp_print_scripts', array($this, 'initialize_daemon_collector'), 99); @@ -286,17 +287,43 @@ if (!class_exists('WC_Retailcrm_Base')) { } $wcCustomer = new WC_Customer($customer_id); - $response = $client->customersList(array('email' => $wcCustomer->get_billing_email())); + $email = $wcCustomer->get_billing_email(); + + if (empty($email)) { + $email = $wcCustomer->get_email(); + } + + if (empty($email)) { + return; + } else { + $wcCustomer->set_billing_email($email); + $wcCustomer->save(); + } + + $response = $client->customersList(array('email' => $email)); if (!empty($response) && $response->isSuccessful() && isset($response['customers']) && count($response['customers']) > 0 ) { - return; - } + $customers = $response['customers']; + $customer = reset($customers); - $this->customers->createCustomer($customer_id); + if (isset($customer['id'])) { + $this->customers->updateCustomerById($customer_id, $customer['id']); + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $builder + ->setWcCustomer($wcCustomer) + ->setPhones(isset($customer['phones']) ? $customer['phones'] : array()) + ->setAddress(isset($customer['address']) ? $customer['address'] : false) + ->build() + ->getResult() + ->save(); + } + } else { + $this->customers->createCustomer($customer_id); + } } /** diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index a718365..ef11dd1 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -185,6 +185,32 @@ if (!class_exists('WC_Retailcrm_Customers')) : return $customer; } + /** + * Update customer in CRM by ID + * + * @param int $customer_id + * @param int|string $crmCustomerId + * + * @return void|\WC_Customer + * @throws \Exception + */ + public function updateCustomerById($customer_id, $crmCustomerId) + { + if (!$this->retailcrm) { + return; + } + + $customer = $this->wcCustomerGet($customer_id); + + if (self::isCustomer($customer)) { + $this->processCustomer($customer); + $this->customer['id'] = $crmCustomerId; + $this->retailcrm->customersEdit($this->customer, 'id'); + } + + return $customer; + } + /** * Create corporate customer in CRM * diff --git a/src/include/components/class-wc-retailcrm-customer-switcher.php b/src/include/components/class-wc-retailcrm-customer-switcher.php index b483c61..5cf7652 100644 --- a/src/include/components/class-wc-retailcrm-customer-switcher.php +++ b/src/include/components/class-wc-retailcrm-customer-switcher.php @@ -51,7 +51,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface $this->data->getWcOrder()->get_id() ) ); - $this->processChangeToRegular($this->data->getWcOrder(), $newCustomer); + $this->processChangeToRegular($this->data->getWcOrder(), $newCustomer, false); $this->data->getWcOrder()->set_billing_company(''); } else { if (!empty($newContact)) { @@ -62,7 +62,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface $this->data->getWcOrder()->get_id() ) ); - $this->processChangeToRegular($this->data->getWcOrder(), $newContact); + $this->processChangeToRegular($this->data->getWcOrder(), $newContact, true); } if (!empty($newCompany)) { @@ -91,10 +91,11 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface * * @param \WC_Order $wcOrder * @param array $newCustomer + * @param bool $isContact * * @throws \WC_Data_Exception */ - public function processChangeToRegular($wcOrder, $newCustomer) + public function processChangeToRegular($wcOrder, $newCustomer, $isContact) { $wcCustomer = null; @@ -145,7 +146,14 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface } $address = self::arrayValue($newCustomer, 'address', array()); - self::setBillingAddressToOrder($wcOrder, $address); + + if ($isContact) { + self::setShippingAddressToOrder($wcOrder, $address); + } else { + self::setBillingAddressToOrder($wcOrder, $address); + self::setShippingAddressToOrder($wcOrder, $address); + } + $wcOrder->set_billing_phone(self::singleCustomerPhone($newCustomer)); $this->result = new WC_Retailcrm_Customer_Switcher_Result($wcCustomer, $wcOrder); @@ -244,6 +252,23 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface $wcOrder->set_billing_address_1(self::arrayValue($address, 'text', '--')); } + /** + * Sets shipping address properties in order + * + * @param \WC_Order $wcOrder + * @param array $address + * + * @throws \WC_Data_Exception + */ + private static function setShippingAddressToOrder($wcOrder, $address) + { + $wcOrder->set_shipping_state(self::arrayValue($address, 'region', '--')); + $wcOrder->set_shipping_postcode(self::arrayValue($address, 'index', '--')); + $wcOrder->set_shipping_country(self::arrayValue($address, 'country', '--')); + $wcOrder->set_shipping_city(self::arrayValue($address, 'city', '--')); + $wcOrder->set_shipping_address_1(self::arrayValue($address, 'text', '--')); + } + /** * @param array|\ArrayObject|\ArrayAccess $arr * @param string $key