diff --git a/resources/pot/retailcrm-ru_RU.pot b/resources/pot/retailcrm-ru_RU.pot index 6658a64..8c451d5 100644 --- a/resources/pot/retailcrm-ru_RU.pot +++ b/resources/pot/retailcrm-ru_RU.pot @@ -549,3 +549,6 @@ msgstr "скидка" msgid "Error while retrieving data. Try again later." msgstr "Ошибка при получении данных. Попробуйте позже." + +msgid "Error when activating the loyalty program. Try again later." +msgstr "Ошибка при активации программы лояльности. Попробуйте позже." diff --git a/src/assets/js/retailcrm-loyalty-actions.js b/src/assets/js/retailcrm-loyalty-actions.js index fb89a0c..cee73ca 100644 --- a/src/assets/js/retailcrm-loyalty-actions.js +++ b/src/assets/js/retailcrm-loyalty-actions.js @@ -48,4 +48,35 @@ jQuery(function() { event.preventDefault(); }); + + jQuery('#loyaltyActivateForm').on("submit", function (event) { + var activateCheckbox = jQuery('#loyaltyActiveCheckbox'); + + if (activateCheckbox.length) { + if (!activateCheckbox.is(':checked')) { + event.preventDefault(); + return false; + } + } + + jQuery.ajax({ + url: LoyaltyUrl.url + '/admin-ajax.php?action=activate_customer_loyalty', + method: 'POST', + timeout: 0, + data: {ajax: 1, loyaltyId: loyaltyId}, + dataType: 'json' + }) + .done(function (response) { + if (response.hasOwnProperty('error')) { + jQuery('#loyaltyRegisterForm').append('

'+ response.error + '

') + + event.preventDefault(); + return false; + } else { + location.reload(); + } + }) + + event.preventDefault(); + }); }); diff --git a/src/include/api/class-wc-retailcrm-client-v5.php b/src/include/api/class-wc-retailcrm-client-v5.php index 540172a..485db22 100644 --- a/src/include/api/class-wc-retailcrm-client-v5.php +++ b/src/include/api/class-wc-retailcrm-client-v5.php @@ -2972,7 +2972,7 @@ class WC_Retailcrm_Client_V5 $parameters['status'] = $status; return $this->client->makeRequest( - "/api/v5/loyalty/account/$clientIdLoyalty/bonus/$status/details", + "/loyalty/account/$clientIdLoyalty/bonus/$status/details", WC_Retailcrm_Request::METHOD_GET, $parameters ); diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index f7961f7..fbbd701 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -91,6 +91,7 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('wp_ajax_clear_cron_tasks', [$this, 'clear_cron_tasks']); add_action('wp_ajax_get_status_coupon', [$this, 'get_status_coupon']); add_action('wp_ajax_register_customer_loyalty', [$this, 'register_customer_loyalty']); + add_action('wp_ajax_activate_customer_loyalty', [$this, 'activate_customer_loyalty']); add_action('admin_print_footer_scripts', [$this, 'ajax_generate_icml'], 99); add_action('woocommerce_update_customer', [$this, 'update_customer'], 10, 1); add_action('user_register', [$this, 'create_customer'], 10, 2); @@ -643,7 +644,7 @@ if (!class_exists('WC_Retailcrm_Base')) { } if (!$userId || !$phone) { - writeBaseLogs('Errors when registering a loyalty program. Passed parameters: userId = ' . $userId ?? 'NULL' . ' phone = ' . $phone ?? 'NULL'); + writeBaseLogs('Errors when registering a loyalty program. Passed parameters: userId = ' . ($userId ?? 'NULL') . ' phone = ' . ($phone ?? 'NULL')); echo json_encode(['error' => __('Error while registering in the loyalty program. Try again later.', 'retailcrm')]); } @@ -658,6 +659,26 @@ if (!class_exists('WC_Retailcrm_Base')) { wp_die(); } + public function activate_customer_loyalty() + { + $loyaltyId = filter_input(INPUT_POST, 'loyaltyId'); + + if (!$loyaltyId) { + writeBaseLogs('Errors when activate loyalty program. loyaltyId is missing'); + echo json_encode(['error' => __('Error when activating the loyalty program. Try again later.', 'retailcrm')]); + } + + $isSuccessful = $this->loyalty->activateLoyaltyCustomer($loyaltyId); + + if (!$isSuccessful) { + echo json_encode(['error' => __('Error when activating the loyalty program. Try again later.', 'retailcrm')]); + } else { + echo json_encode(['isSuccessful' => true]); + } + + wp_die(); + } + /** * In this method we include CSS file * @@ -885,10 +906,11 @@ if (!class_exists('WC_Retailcrm_Base')) { $result = $this->loyalty->getForm($userId); - if (!isset($result)) { + if ([] === $result) { echo '

'. __('Error while retrieving data. Try again later.', 'retailcrm') . '

'; } else { - echo $result; + wp_localize_script($jsScript, 'loyaltyId', $result['loyaltyId'] ?? null); + echo $result['form']; } } diff --git a/src/include/class-wc-retailcrm-loyalty.php b/src/include/class-wc-retailcrm-loyalty.php index 2cb0e20..599bead 100644 --- a/src/include/class-wc-retailcrm-loyalty.php +++ b/src/include/class-wc-retailcrm-loyalty.php @@ -29,7 +29,7 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : public function getForm(int $userId) { - $result = null; + $result = []; $response = $this->apiClient->customersGet($userId); @@ -55,22 +55,22 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : if (isset($response['loyaltyAccounts'][0]) && (int)$loyaltyAccount['customer']['externalId'] === $userId) { if ($loyaltyAccount['active'] === true) { - $result = $this->getLoyaltyInfo($loyaltyAccount); + $result['form'] = $this->getLoyaltyInfo($loyaltyAccount); } else { - $result = sprintf( + $result['form'] = sprintf( '
- - -
+

%s

', __('Activate participation in the loyalty program', 'retailcrm'), __('Send', 'retailcrm') ); + + $result['loyaltyId'] = $loyaltyAccount['id']; } } else { - $result = sprintf( + $result['form'] = sprintf( '

%s

@@ -116,6 +116,23 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : } } + public function activateLoyaltyCustomer(int $loyaltyId) + { + try { + $response = $this->apiClient->activateLoyaltyAccount($loyaltyId); + + if (!$response->isSuccessful()) { + writeBaseLogs('Error while registering in the loyalty program: ' . $response->getRawResponse()); + } + + return $response->isSuccessful(); + } catch (Throwable $exception) { + writeBaseLogs('Exception while activate loyalty account: ' . $exception->getMessage()); + + return false; + } + } + private function getLoyaltyInfo(array $loyaltyAccount) { $data = [ diff --git a/src/languages/retailcrm-ru_RU.mo b/src/languages/retailcrm-ru_RU.mo index 4abcc17..7ca912e 100644 Binary files a/src/languages/retailcrm-ru_RU.mo and b/src/languages/retailcrm-ru_RU.mo differ