From af910c935317726ab8d4be6dba0f493a27054068 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 13 Feb 2020 13:27:56 +0300 Subject: [PATCH] extract customer data from order for guests --- src/include/class-wc-retailcrm-customers.php | 33 +++++++++++++++---- src/include/class-wc-retailcrm-orders.php | 2 +- .../class-wc-retailcrm-customer-address.php | 29 +++++++++++----- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index f83ddf0..b6b6894 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -130,9 +130,12 @@ if (!class_exists('WC_Retailcrm_Customers')) : * * @param int | WC_Customer $customer * + * @param \WC_Order|null $order + * * @return mixed + * @throws \Exception */ - public function createCustomer($customer) + public function createCustomer($customer, $order = null) { if (!$this->retailcrm) { return null; @@ -147,7 +150,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : } if (self::isCustomer($customer)) { - $this->processCustomer($customer); + $this->processCustomer($customer, $order); $response = $this->retailcrm->customersCreate($this->customer); if ($response->isSuccessful() && isset($response['id'])) { @@ -255,15 +258,31 @@ if (!class_exists('WC_Retailcrm_Customers')) : /** * Process customer * - * @param WC_Customer $customer + * @param WC_Customer $customer + * @param WC_Order|null $order * * @return void * @throws \Exception */ - protected function processCustomer($customer) + protected function processCustomer($customer, $order = null) { $createdAt = $customer->get_date_created(); $firstName = $customer->get_first_name(); + $lastName = $customer->get_last_name(); + + if (empty($firstName) && empty($lastName) && $order instanceof WC_Order) { + $firstName = $order->get_billing_first_name(); + $lastName = $order->get_billing_last_name(); + + if (empty($firstName) && empty($lastName)) { + $firstName = $order->get_shipping_first_name(); + $lastName = $order->get_shipping_last_name(); + } + + if (empty($firstName)) { + $firstName = $customer->get_username(); + } + } if (empty($createdAt)) { $createdAt = new WC_DateTime(); @@ -271,10 +290,10 @@ if (!class_exists('WC_Retailcrm_Customers')) : $data_customer = array( 'createdAt' => $createdAt->date('Y-m-d H:i:s'), - 'firstName' => $firstName ? $firstName : $customer->get_username(), - 'lastName' => $customer->get_last_name(), + 'firstName' => $firstName, + 'lastName' => $lastName, 'email' => $customer->get_email(), - 'address' => $this->customer_address->build($customer)->get_data() + 'address' => $this->customer_address->build($customer, $order)->get_data() ); if ($customer->get_id() > 0) { diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 6a0a5c5..6398c6d 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -213,7 +213,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $foundCustomer = $this->customers->findCustomerEmailOrId($wcCustomerId, $wcCustomerEmail); if (empty($foundCustomer)) { - $foundCustomerId = $this->customers->createCustomer($wcCustomerId); + $foundCustomerId = $this->customers->createCustomer($wcCustomerId, $wcOrder); if (!empty($foundCustomerId)) { $this->order['customer']['id'] = $foundCustomerId; diff --git a/src/include/customer/class-wc-retailcrm-customer-address.php b/src/include/customer/class-wc-retailcrm-customer-address.php index 621fc72..ddb2f8b 100644 --- a/src/include/customer/class-wc-retailcrm-customer-address.php +++ b/src/include/customer/class-wc-retailcrm-customer-address.php @@ -17,19 +17,30 @@ class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address protected $filter_name = 'customer_address'; /** - * @param WC_Customer $customer + * @param WC_Customer $customer + * @param \WC_Order|null $order * * @return self */ - public function build($customer) + public function build($customer, $order = null) { - $data = array( - 'index' => $customer->get_billing_postcode(), - 'countryIso' => $customer->get_billing_country(), - 'region' => $customer->get_billing_state(), - 'city' => $customer->get_billing_city(), - 'text' => $customer->get_billing_address_1() . ', ' . $customer->get_billing_address_2() - ); + if ($order instanceof WC_Order && empty($customer->get_billing_address())) { + $data = array( + 'index' => $order->get_billing_postcode(), + 'countryIso' => $order->get_billing_country(), + 'region' => $order->get_billing_state(), + 'city' => $order->get_billing_city(), + 'text' => $order->get_billing_address_1() . ', ' . $order->get_billing_address_2() + ); + } else { + $data = array( + 'index' => $customer->get_billing_postcode(), + 'countryIso' => $customer->get_billing_country(), + 'region' => $customer->get_billing_state(), + 'city' => $customer->get_billing_city(), + 'text' => $customer->get_billing_address_1() . ', ' . $customer->get_billing_address_2() + ); + } $this->set_data_fields($data);