diff --git a/src/include/class-wc-retailcrm-loyalty.php b/src/include/class-wc-retailcrm-loyalty.php index 2cc51ff..f6eb2fb 100644 --- a/src/include/class-wc-retailcrm-loyalty.php +++ b/src/include/class-wc-retailcrm-loyalty.php @@ -346,25 +346,19 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : return $discountLp; } - public function isValidOrder($wcUser, $wcOrder) + public function isValidOrder($wcCustomer, $wcOrder) { - if (!$wcUser) { - return false; - } - - $wcCustomer = new WC_Customer($wcUser->ID); - return !(!$wcCustomer || (isCorporateUserActivate($this->settings) && isCorporateOrder($wcCustomer, $wcOrder))); } - public function isValidUser($wcUser) + public function isValidUser($wcCustomer) { - if (empty($wcUser)) { + if (empty($wcCustomer)) { return false; } try { - $response = $this->getLoyaltyAccounts($wcUser->ID); + $response = $this->getLoyaltyAccounts($wcCustomer->get_id()); } catch (Throwable $exception) { writeBaseLogs('Exception get loyalty accounts: ' . $exception->getMessage()); diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 45d9275..9d0abb0 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -104,16 +104,20 @@ if (!class_exists('WC_Retailcrm_Orders')) : if ($this->loyalty) { $privilegeType = 'none'; $discountLp = $this->loyalty->deleteLoyaltyCouponInOrder($wcOrder); - $wcUser = $wcOrder->get_user(); + $data = $wcOrder->get_data(); - if (!$this->loyalty->isValidOrder($wcUser, $wcOrder)) { + if (isset($data['customer_id'])) { + $wcCustomer = new WC_Customer($data['customer_id']); + } + + if (!$this->loyalty->isValidOrder($wcCustomer ?? null, $wcOrder)) { if ($discountLp > 0) { writeBaseLogs('The user does not meet the requirements for working with the loyalty program. Order Id: ' . $orderId); } $discountLp = 0; $privilegeType = 'none'; - } elseif ($this->loyalty->isValidUser($wcUser)) { + } elseif ($this->loyalty->isValidUser($wcCustomer ?? null)) { $privilegeType = 'loyalty_level'; } } diff --git a/tests/test-wc-retailcrm-loyalty.php b/tests/test-wc-retailcrm-loyalty.php index cdd70ad..c511081 100644 --- a/tests/test-wc-retailcrm-loyalty.php +++ b/tests/test-wc-retailcrm-loyalty.php @@ -121,12 +121,6 @@ class WC_Retailcrm_Loyalty_Test extends WC_Retailcrm_Test_Case_Helper */ public function testIsValidOrder($customer, $corporate_enabled, $expected, $orderCorporate) { - $wp_user = null; - - if ($customer) { - $wp_user = get_user_by('ID', $customer->get_id()); - } - $this->loyalty = new WC_Retailcrm_Loyalty($this->apiMock, ['corporate_enabled' => $corporate_enabled]); $wcOrder = new WC_Order(); @@ -134,7 +128,7 @@ class WC_Retailcrm_Loyalty_Test extends WC_Retailcrm_Test_Case_Helper $wcOrder->set_billing_company('OOO TEST'); } - $this->assertEquals($expected, $this->loyalty->isValidOrder($wp_user, $wcOrder)); + $this->assertEquals($expected, $this->loyalty->isValidOrder($customer, $wcOrder)); } /**