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

orders upload with corporate customers

This commit is contained in:
Pavel 2019-11-19 11:09:22 +03:00 committed by Павел
parent d8f89c1794
commit b4200c952f
4 changed files with 155 additions and 48 deletions

View file

@ -54,6 +54,22 @@ if (!class_exists('WC_Retailcrm_Customers')) :
$this->customer_address = $customer_address;
}
/**
* setCustomerAddress
*
* @param $address
*
* @return $this
*/
public function setCustomerAddress($address)
{
if ($address instanceof WC_Retailcrm_Customer_Address) {
$this->customer_address = $address;
}
return $this;
}
/**
* Returns true if corporate customers are enabled and accessible
*

View file

@ -174,6 +174,17 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
$history = $response['history'];
$end_change = end($history);
$new_since_id = $end_change['id'];
$mapping = array(
'first_name' => 'first_name',
'last_name' => 'last_name',
'email' => 'billing_email',
'phones' => 'billing_phone',
'address.region' => 'billing_state',
'address.index' => 'billing_postcode',
'address.country' => 'billing_country',
'address.city' => 'billing_city'
);
foreach ($history as $record) {
if ($record['source'] == 'api' && $record['apiKey']['current'] == true) {
@ -196,51 +207,15 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
WC_Retailcrm_Plugin::$history_run = true;
if ($record['field'] == 'first_name' && isset($record['customer']['externalId'])) {
if ($record['newValue']){
update_user_meta($record['customer']['externalId'], 'first_name', $record['newValue']);
}
}
elseif ($record['field'] == 'last_name' && isset($record['customer']['externalId'])) {
if (isset($mapping[$record['field']]) && isset($record['customer']['externalId'])) {
if ($record['newValue']) {
update_user_meta($record['customer']['externalId'], 'last_name', $record['newValue']);
}
}
elseif ($record['field'] == 'email' && isset($record['customer']['externalId'])) {
if ($record['newValue']){
update_user_meta($record['customer']['externalId'], 'billing_email', $record['newValue']);
}
}
elseif ($record['field'] == 'phones' && isset($record['customer']['externalId'])) {
if ($record['newValue']){
update_user_meta($record['customer']['externalId'], 'billing_phone', $record['newValue']);
}
}
elseif ($record['field'] == 'address.region' && isset($record['customer']['externalId'])) {
if ($record['newValue']){
update_user_meta($record['customer']['externalId'], 'billing_state', $record['newValue']);
}
}
elseif ($record['field'] == 'address.index' && isset($record['customer']['externalId'])) {
if ($record['newValue']){
update_user_meta($record['customer']['externalId'], 'billing_postcode', $record['newValue']);
}
}
elseif ($record['field'] == 'address.country' && isset($record['customer']['externalId'])) {
if ($record['newValue']){
update_user_meta($record['customer']['externalId'], 'billing_country', $record['newValue']);
}
}
elseif ($record['field'] == 'address.city' && isset($record['customer']['externalId'])) {
if ($record['newValue']){
update_user_meta($record['customer']['externalId'], 'billing_city', $record['newValue']);
if (is_array($mapping[$record['field']])) {
foreach ($mapping[$record['field']] as $mappingField) {
update_user_meta($record['customer']['externalId'], $mappingField, $record['newValue']);
}
} else {
update_user_meta($record['customer']['externalId'], $mapping[$record['field']], $record['newValue']);
}
}
}

View file

@ -14,7 +14,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
*/
class WC_Retailcrm_Orders
{
/** @var bool|WC_Retailcrm_Proxy */
/** @var bool|WC_Retailcrm_Proxy|\WC_Retailcrm_Client_V5|\WC_Retailcrm_Client_V4 */
protected $retailcrm;
/** @var array */
@ -72,6 +72,13 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
return null;
}
$isCorporateEnabled = WC_Retailcrm_Customers::isCorporateEnabledInApi($this->retailcrm);
$uploader = new WC_Retailcrm_Customers(
$this->retailcrm,
$this->retailcrm_settings,
new WC_Retailcrm_Customer_Address()
);
$orders = get_posts(array(
'numberposts' => -1,
'post_type' => wc_get_order_types('view-orders'),
@ -80,6 +87,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
));
$orders_data = array();
$orders_data_corps = array();
foreach ($orders as $data_order) {
$order = wc_get_order($data_order->ID);
@ -95,20 +103,96 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
}
}
$orders_data[] = $this->order;
if ($isCorporateEnabled
&& WC_Retailcrm_Customers::customerPossiblyCorporate(new WC_Customer($customer->get('ID')))
) {
$corporate = $this->retailcrm->customersCorporateGet($customer->get('ID'));
time_nanosleep(0, 100000000);
if ($corporate instanceof WC_Retailcrm_Response
&& $corporate->isSuccessful()
&& $corporate->offsetExists('customerCorporate')
&& isset($corporate['customerCorporate']['mainCustomerContact'])
&& isset($corporate['customerCorporate']['mainCustomerContact']['customer'])
&& isset($corporate['customerCorporate']['mainCustomerContact']['customer']['id'])
&& isset($corporate['customerCorporate']['mainCustomerContact']['customer']['externalId'])
) {
$this->order['contact'] = array(
'id' =>
$corporate['customerCorporate']['mainCustomerContact']['customer']['id']
);
$orders_data_corps[] = $this->order;
} else {
$orders_data[] = $this->order;
}
} else {
$orders_data[] = $this->order;
}
}
$corporateCustomers = array();
if ($withCustomers === true && !empty($customers)) {
$this->customers->customersUpload($customers);
if ($isCorporateEnabled) {
foreach ($customers as $key => $customer) {
if (WC_Retailcrm_Customers::customerPossiblyCorporate(new WC_Customer($customer))) {
$corporateCustomers[] = $customer;
unset($customers[$key]);
}
}
$uploadCustomers = array_chunk($customers, 50);
foreach ($uploadCustomers as $uploadCustomer) {
$this->customers->customersUpload($uploadCustomer);
time_nanosleep(0, 250000000);
}
foreach ($corporateCustomers as $corporateCustomer) {
$response = $uploader->createCorporateCustomer(new WC_Customer($corporateCustomer));
if ($response instanceof WC_Retailcrm_Customer_Corporate_Response) {
if ($response->getContactId() != 0) {
foreach ($orders_data as $key => $order) {
if (isset($order['customer']['externalId'])
&& $order['customer']['externalId'] == $response->getExternalId()
) {
$order['contact'] = array(
'id' => $response->getContactId()
);
$orders_data_corps[] = $order;
unset($orders_data[$key]);
break;
}
}
}
}
}
} else {
$uploadCustomers = array_chunk($customers, 50);
foreach ($uploadCustomers as $uploadCustomer) {
$this->customers->customersUpload($uploadCustomer);
time_nanosleep(0, 250000000);
}
}
}
$uploadOrders = array_chunk($orders_data, 50);
$uploadOrders = array_chunk(WC_Retailcrm_Plugin::clearArray($orders_data), 50);
$uploadOrdersCorps = array_chunk(WC_Retailcrm_Plugin::clearArray($orders_data_corps), 50);
foreach ($uploadOrders as $uploadOrder) {
$this->retailcrm->ordersUpload($uploadOrder);
time_nanosleep(0, 250000000);
}
foreach ($uploadOrdersCorps as $uploadOrdersCorp) {
$this->retailcrm->ordersUpload($uploadOrdersCorp);
time_nanosleep(0, 250000000);
}
return $uploadOrders;
}

View file

@ -127,6 +127,38 @@ class WC_Retailcrm_Plugin {
return false;
}
/**
* Unset empty fields
*
* @param array $arr input array
*
* @return array
*/
public static function clearArray(array $arr)
{
if (!is_array($arr)) {
return $arr;
}
$result = [];
foreach ($arr as $index => $node) {
$result[$index] = (is_array($node))
? self::clearArray($node)
: $node;
if ($result[$index] == ''
|| $result[$index] === null
|| (is_array($result[$index]) && count($result[$index]) < 1)
) {
unset($result[$index]);
}
}
return $result;
}
/**
* Check running history
*