From a639f1b6e15603d319d078944236af42e27bbe80 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 17 Feb 2020 11:00:23 +0300 Subject: [PATCH] fix for customer id assigning & possible crash after errors in order creation --- src/include/class-wc-retailcrm-history.php | 22 ++++++++++++++++++---- src/include/class-wc-retailcrm-plugin.php | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 14078bd..1fc8ff3 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -522,22 +522,36 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : return false; } - if (empty($order['customer']['externalId'])) + $customerId = isset($order['customer']['externalId']) ? $order['customer']['externalId'] : null; + + if ($order['customer']['type'] == 'customer_corporate') { + $customerId = isset($order['contact']['externalId']) ? $order['contact']['externalId'] : null; + } $args = array( 'status' => isset($options[$order['status']]) ? $options[$order['status']] : 'processing', - 'customer_id' => isset($order['customer']['externalId']) - ? $order['customer']['externalId'] - : (isset($order['contact']['externalId']) ? $order['contact']['externalId'] : null) + 'customer_id' => $customerId ); + /** @var WC_Order|WP_Error $wc_order */ $wc_order = wc_create_order($args); $customer = $order['customer']; $address = isset($order['customer']['address']) ? $order['customer']['address'] : array(); $companyName = ''; + if ($wc_order instanceof WP_Error) { + $logger = new WC_Logger(); + $logger->add('retailcrm', sprintf( + '[%d] error while creating order: %s', + $order['id'], + print_r($wc_order->get_error_messages(), true) + )); + + return false; + } + if (isset($order['customer']['type']) && $order['customer']['type'] == 'customer_corporate' && !empty($order['customer']['mainCompany']) diff --git a/src/include/class-wc-retailcrm-plugin.php b/src/include/class-wc-retailcrm-plugin.php index 8024d26..a4f3e7d 100644 --- a/src/include/class-wc-retailcrm-plugin.php +++ b/src/include/class-wc-retailcrm-plugin.php @@ -90,7 +90,7 @@ class WC_Retailcrm_Plugin { /** * Edit configuration in CRM * - * @param WC_Retailcrm_Proxy $api_client + * @param WC_Retailcrm_Proxy|\WC_Retailcrm_Client_V4|\WC_Retailcrm_Client_V5 $api_client * @param string $client_id * @param bool $active *