diff --git a/.env-dist b/.env-dist index 17a33c9..f89fd7b 100644 --- a/.env-dist +++ b/.env-dist @@ -1,6 +1,12 @@ +# MySQL host and credentials DB_NAME=wc_retailcrm_test DB_USER=wc_retailcrm DB_PASS=wc_retailcrm DB_HOST=mysql + +# WordPress and WooCommerce versions WP_VERSION=4.4 WC_VERSION=3.0.0 + +# Enable this in order to pipe all module log messages (including debug ones) to STDOUT. +MODULE_LOGS_TO_STDOUT=0 diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 37ce855..1e3718e 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -35,6 +35,9 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : /** @var WC_Retailcrm_Order */ protected $orders; + /** @var array */ + private $ordersGetRequestCache = array(); + /** @var array */ private $order = array(); @@ -274,10 +277,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $wpUser = $wcOrder->get_user(); if ($update) { - $response = $this->retailcrm->ordersGet($wcOrder->get_id()); + $response = $this->getCrmOrder($wcOrder->get_id()); - if (!empty($response) && $response->isSuccessful() && isset($response['order'])) { - $customerWasChanged = self::isOrderCustomerWasChanged($wcOrder, $response['order']); + if (!empty($response)) { + $customerWasChanged = self::isOrderCustomerWasChanged($wcOrder, $response); } } @@ -299,6 +302,11 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } } + if ($update && $customerWasChanged) { + $this->order['firstName'] = $wcOrder->get_shipping_first_name(); + $this->order['lastName'] = $wcOrder->get_shipping_last_name(); + } + return true; } @@ -470,11 +478,16 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : return null; } +<<<<<<< HEAD $response = $this->retailcrm->ordersGet($order->get_id()); if (!empty($response) && $response->isSuccessful()) { $retailcrmOrder = $response['order']; +======= + $retailcrmOrder = $this->getCrmOrder($order->get_id()); +>>>>>>> several fixes & environment variable which can be used to output logs to stdout in tests + if (!empty($retailcrmOrder)) { foreach ($retailcrmOrder['payments'] as $payment_data) { $payment_external_id = explode('-', $payment_data['externalId']); @@ -614,6 +627,31 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : return $payment; } + /** + * ordersGet wrapper with cache (in order to minimize request count). + * + * @param int|string $orderId + * @param bool $cached + * + * @return array + */ + protected function getCrmOrder($orderId, $cached = true) + { + if ($cached && isset($this->ordersGetRequestCache[$orderId])) { + return (array) $this->ordersGetRequestCache[$orderId]; + } + + $crmOrder = array(); + $response = $this->retailcrm->ordersGet($orderId); + + if (!empty($response) && $response->isSuccessful() && isset($response['order'])) { + $crmOrder = (array) $response['order']; + $this->ordersGetRequestCache[$orderId] = $crmOrder; + } + + return $crmOrder; + } + /** * @return array */ @@ -678,6 +716,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : */ public static function isOrderCustomerWasChanged($wcOrder, $crmOrder) { + if (!isset($crmOrder['customer'])) { + return false; + } + $customerWasChanged = self::isCorporateOrder($wcOrder) != self::isCorporateCrmOrder($crmOrder); $synchronizableUserData = self::isCorporateCrmOrder($crmOrder) ? $crmOrder['contact'] : $crmOrder['customer']; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 1aca407..093598d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,6 +1,7 @@