1
0
Fork 0
mirror of synced 2025-04-06 07:13:33 +03:00

fixes for changed customer sync & correctly load created customer

This commit is contained in:
Pavel 2020-06-16 15:05:27 +03:00
parent 34a235510c
commit 678c2d0820
3 changed files with 86 additions and 8 deletions

View file

@ -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);
}
}
/**

View file

@ -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
*

View file

@ -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