diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index 95be386..219f185 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -17,17 +17,30 @@ if (!class_exists('WC_Retailcrm_Base')) { */ class WC_Retailcrm_Base extends WC_Retailcrm_Abstracts_Settings { + /** @var string */ protected $api_url; + + /** @var string */ protected $api_key; + + /** @var \WC_Retailcrm_Proxy|WC_Retailcrm_Client_V4|WC_Retailcrm_Client_V5|bool */ protected $apiClient; + + /** @var mixed */ protected $order_item; + + /** @var mixed */ protected $order_address; + + /** @var \WC_Retailcrm_Customers */ protected $customers; + + /** @var \WC_Retailcrm_Orders */ protected $orders; /** * Init and hook in the integration. - * @param $retailcrm (default = false) + * @param \WC_Retailcrm_Proxy|WC_Retailcrm_Client_V4|WC_Retailcrm_Client_V5|bool $retailcrm (default = false) */ public function __construct($retailcrm = false) { parent::__construct(); @@ -228,11 +241,11 @@ if (!class_exists('WC_Retailcrm_Base')) { } } - $ids = array_merge($ids, $appendix); + $ids = array_unique(array_merge($ids, $appendix)); } if ($ids) { - $this->orders->ordersUpload(array_unique($ids), true); + $this->orders->ordersUpload($ids); } } diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 963a5f9..7e5f185 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -62,13 +62,12 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : /** * Upload orders to CRM * - * @param bool $withCustomers * @param array $include * * @return array $uploadOrders | null * @throws \Exception */ - public function ordersUpload($include = array(), $withCustomers = false) + public function ordersUpload($include = array()) { if (!$this->retailcrm) { return null; @@ -87,54 +86,27 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : 'include' => $include )); - $ordersData = array(); + $regularUploadErrors = array(); $corporateUploadErrors = array(); foreach ($orders as $data_order) { $order = wc_get_order($data_order->ID); - if ($this->retailcrm->getCorporateEnabled() && self::isCorporateOrder($order)) { - $errorMessage = $this->orderCreate($data_order->ID); + $errorMessage = $this->orderCreate($data_order->ID); - if (is_string($errorMessage)) { + if (is_string($errorMessage)) { + if ($this->retailcrm->getCorporateEnabled() && self::isCorporateOrder($order)) { $corporateUploadErrors[$data_order->ID] = $errorMessage; - } - - continue; - } - - $customer = $order->get_user(); - $this->processOrder($order); - $customers = array(); - - if ($customer != false) { - $this->order['customer']['externalId'] = $customer->get('ID'); - - if ($withCustomers === true) { - $customers[] = $customer->get('ID'); + } else { + $regularUploadErrors[$data_order->ID] = $errorMessage; } } - - $ordersData[] = $this->order; } - if ($withCustomers === true && !empty($customers)) { - $uploadCustomers = array_chunk($customers, 50); + static::logOrdersUploadErrors($regularUploadErrors, 'Error while uploading these regular orders'); + static::logOrdersUploadErrors($corporateUploadErrors, 'Error while uploading these corporate orders'); - foreach ($uploadCustomers as $uploadCustomer) { - $this->customers->customersUpload($uploadCustomer); - time_nanosleep(0, 250000000); - } - } - - $uploadOrders = array_chunk(WC_Retailcrm_Plugin::clearArray($ordersData), 50); - - foreach ($uploadOrders as $uploadOrder) { - $this->retailcrm->ordersUpload($uploadOrder); - time_nanosleep(0, 250000000); - } - - return $uploadOrders; + return array(); } /** @@ -151,6 +123,8 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : return null; } + $this->order_payment->reset_data(); + $wcOrder = wc_get_order($order_id); $this->processOrder($wcOrder); $wpUser = $wcOrder->get_user(); @@ -241,7 +215,9 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : break; } } - } else { + } + + if (empty($crmCorporate)) { $crmCorporate = $this ->customers ->findCorporateCustomerByMainCompany($wcOrder->get_billing_company()); @@ -465,5 +441,29 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : { return !empty($order->get_billing_company()); } + + /** + * Logs orders upload errors with prefix log message. + * Array keys must be orders ID's in WooCommerce, values must be strings (error messages). + * + * @param array $errors + * @param string $prefix + */ + public static function logOrdersUploadErrors($errors, $prefix = 'Errors while uploading these orders') + { + if (empty($errors)) { + return; + } + + $handle = 'retailcrm'; + $logger = new WC_Logger(); + $logger->add($handle, $prefix); + + foreach ($errors as $orderId => $error) { + $logger->add($handle, sprintf("[%d] => %s", $orderId, $error)); + } + + $logger->add($handle, '=================================='); + } } endif; diff --git a/src/include/order/class-wc-retailcrm-order-payment.php b/src/include/order/class-wc-retailcrm-order-payment.php index f16bf9b..6171154 100644 --- a/src/include/order/class-wc-retailcrm-order-payment.php +++ b/src/include/order/class-wc-retailcrm-order-payment.php @@ -81,7 +81,7 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data } } - $this->set_data_fields($data); + $this->set_data_fields(WC_Retailcrm_Plugin::clearArray($data)); return $this; }