diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index f7e1417..03d9f0f 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 ((!empty($response) && $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,17 @@ if (!class_exists('WC_Retailcrm_Customers')) : $data_customer = array( 'createdAt' => $createdAt->date('Y-m-d H:i:s'), +<<<<<<< HEAD 'firstName' => $firstName ? $firstName : $customer->get_username(), 'lastName' => $customer->get_last_name(), 'email' => $customer->get_billing_email(), 'address' => $this->customer_address->build($customer)->get_data() +======= + 'firstName' => $firstName, + 'lastName' => $lastName, + 'email' => $customer->get_email(), + 'address' => $this->customer_address->build($customer, $order)->get_data() +>>>>>>> extract customer data from order for guests ); if ($customer->get_id() > 0) { diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 6cf021a..6c73a13 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -295,7 +295,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 198f170..69a20c6 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' => $this->get_state_name($customer->get_billing_country(), $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' => $this->get_state_name($order->get_billing_country(), $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' => $this->get_state_name($customer->get_billing_country(), $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);