From 7c84f365f0dccb73bd3d99dc35ba73476ba6e894 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Thu, 11 Apr 2024 17:55:44 +0300 Subject: [PATCH] ref #72067 Small corrections Added possibility to modify loyalty program forms --- resources/pot/retailcrm-ru_RU.pot | 60 ++++++------- src/assets/js/retailcrm-loyalty-actions.js | 2 +- src/include/class-wc-retailcrm-base.php | 26 +++--- src/include/class-wc-retailcrm-loyalty.php | 85 +++--------------- .../class-wc-retailcrm-loyalty-form.php | 90 +++++++++++++++++++ src/retailcrm.php | 1 + 6 files changed, 143 insertions(+), 121 deletions(-) create mode 100644 src/include/components/class-wc-retailcrm-loyalty-form.php diff --git a/resources/pot/retailcrm-ru_RU.pot b/resources/pot/retailcrm-ru_RU.pot index bee1b2d..6b60376 100644 --- a/resources/pot/retailcrm-ru_RU.pot +++ b/resources/pot/retailcrm-ru_RU.pot @@ -481,26 +481,26 @@ msgstr "Для активации программы лояльности нео msgid "Bonus account" msgstr "Бонусный счёт" -msgid "Participation ID" -msgstr "ID участия" +msgid "Participation ID: " +msgstr "ID участия: " -msgid "Current level" -msgstr "Текущий уровень" +msgid "Current level: " +msgstr "Текущий уровень: " -msgid "Bonuses on the account" -msgstr "Бонусов на счёте" +msgid "Bonuses on the account: " +msgstr "Бонусов на счёте: " -msgid "Bonus card number" -msgstr "Номер бонусной карты" +msgid "Bonus card number: " +msgstr "Номер бонусной карты: " -msgid "Date of registration" -msgstr "Дата регистрации" +msgid "Date of registration: " +msgstr "Дата регистрации: " msgid "Current level rules" msgstr "Правила текущего уровня" -msgid "Required amount of purchases to move to the next level" -msgstr "Необходимая сумма покупок для перехода на следующий уровень" +msgid "Required amount of purchases to move to the next level: " +msgstr "Необходимая сумма покупок для перехода на следующий уровень: " msgid "Activate participation in the loyalty program" msgstr "Активировать участие в программе лояльности" @@ -529,24 +529,6 @@ msgstr "Ошибка при регистрации в программе лоя msgid "The card is not linked" msgstr "Карта не привязана" -msgid "Ordinary goods" -msgstr "Обычные товары" - -msgid "Promotional products" -msgstr "Акционные товары" - -msgid "accrual of 1 bonus for each" -msgstr "начисление 1 бонуса за каждые" - -msgid "of the purchase amount" -msgstr "от суммы покупки" - -msgid "bonus accrual in the amount of" -msgstr "начисление бонусов в размере" - -msgid "discount" -msgstr "скидка" - msgid "Error while retrieving data. Try again later" msgstr "Ошибка при получении данных. Попробуйте позже" @@ -558,3 +540,21 @@ msgstr "Введите корректный номер телефона" msgid "Close" msgstr "Закрыть" + +msgid "Ordinary products: accrual of 1 bonus for each %s %s" +msgstr "Обычные товары: начисление 1 бонуса за каждые %s %s" + +msgid "Promotional products: accrual of 1 bonus for each %s %s" +msgstr "Акционные товары: начисление 1 бонуса за каждые %s %s" + +msgid "Ordinary products: bonus accrual in the amount of %s% of the purchase amount" +msgstr "Обычные товары: начисление бонусов в размере %s% от суммы покупки" + +msgid "Promotional products: bonus accrual in the amount of %s% of the purchase amount" +msgstr "Акционные товары: начисление бонусов в размере %s% от суммы покупки" + +msgid "Ordinary products: %s% discount" +msgstr "Обычные товары: %s% скидка" + +msgid "Promotional products: %s% discount" +msgstr "Акционные товары: %s% скидка" diff --git a/src/assets/js/retailcrm-loyalty-actions.js b/src/assets/js/retailcrm-loyalty-actions.js index c75f08c..a45233f 100644 --- a/src/assets/js/retailcrm-loyalty-actions.js +++ b/src/assets/js/retailcrm-loyalty-actions.js @@ -100,7 +100,7 @@ jQuery(function() { }); jQuery(document).keydown(function(e) { - if (e.keyCode === 27) { + if (e.keyCode === 27) { // Key Escape e.stopPropagation(); jQuery('.popup-fade-loyalty').fadeOut(); } diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index b24c4dd..7fbbad6 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -636,22 +636,17 @@ if (!class_exists('WC_Retailcrm_Base')) { { $phone = filter_input(INPUT_POST, 'phone'); $userId = filter_input(INPUT_POST, 'userId'); - $site = $this->apiClient->getSingleSiteForKey(); + $isSuccessful = false; - if (empty($site)) { - writeBaseLogs('Error with CRM credentials: need an valid apiKey assigned to one certain site'); - echo json_encode(['error' => __('Error while registering in the loyalty program. Try again later', 'retailcrm')]); + if (!empty($site) && $userId && $phone) { + $isSuccessful = $this->loyalty->registerCustomer($userId, $phone, $site); } - if (!$userId || !$phone) { - 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')]); - } - - $isSuccessful = $this->loyalty->registerCustomer($userId, $phone, $site); - if (!$isSuccessful) { + writeBaseLogs('Errors when registering a loyalty program. Passed parameters: ' . + json_encode(['site' => $site, 'userId' => $userId, 'phone' => $phone]) + ); echo json_encode(['error' => __('Error while registering in the loyalty program. Try again later', 'retailcrm')]); } else { echo json_encode(['isSuccessful' => true]); @@ -663,15 +658,14 @@ if (!class_exists('WC_Retailcrm_Base')) { public function activate_customer_loyalty() { $loyaltyId = filter_input(INPUT_POST, 'loyaltyId'); + $isSuccessful = false; - 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')]); + if ($loyaltyId) { + $isSuccessful = $this->loyalty->activateLoyaltyCustomer($loyaltyId); } - $isSuccessful = $this->loyalty->activateLoyaltyCustomer($loyaltyId); - if (!$isSuccessful) { + writeBaseLogs('Errors when activate loyalty program. Passed parameters: ' . json_encode(['loyaltyId' => $loyaltyId])); echo json_encode(['error' => __('Error when activating the loyalty program. Try again later', 'retailcrm')]); } else { echo json_encode(['isSuccessful' => true]); diff --git a/src/include/class-wc-retailcrm-loyalty.php b/src/include/class-wc-retailcrm-loyalty.php index db4d338..936ec3d 100644 --- a/src/include/class-wc-retailcrm-loyalty.php +++ b/src/include/class-wc-retailcrm-loyalty.php @@ -1,6 +1,9 @@ apiClient = $apiClient; $this->settings = $settings; $this->dateFormat = 'Y-m-d H:i:sP'; + $this->loyaltyForm = new WC_Retailcrm_Loyalty_Form(); } public function getForm(int $userId) @@ -55,47 +63,14 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : if ($loyaltyAccount && (int) $loyaltyAccount['customer']['externalId'] === $userId) { if ($loyaltyAccount['active'] === true) { - $result['form'] = $this->getLoyaltyInfo($loyaltyAccount); + $result['form'] = $this->loyaltyForm->getInfoLoyalty($loyaltyAccount); } else { - $result['form'] = sprintf( - ' -
-

%s

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

%s

-

%s%s.

-

%s%s.

-

-

-
- - ', - __('To register in the loyalty program, fill in the form:', 'retailcrm'), - __(' I agree with ', 'retailcrm'), - __('loyalty program terms', 'retailcrm'), - __(' I agree with ', 'retailcrm'), - __('terms of personal data processing', 'retailcrm'), - __('Phone', 'retailcrm'), - __('Send', 'retailcrm'), - __('Close', 'retailcrm') - ); + $result['form'] = $this->loyaltyForm->getRegistrationForm(); } return $result; @@ -141,44 +116,6 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : return false; } } - - private function getLoyaltyInfo(array $loyaltyAccount) - { - $data = [ - '' . __('Bonus account', 'retailcrm') . '', - __('Participation ID', 'retailcrm') . ': ' . $loyaltyAccount['id'], - __('Current level', 'retailcrm') . ': ' . $loyaltyAccount['level']['name'], - __('Bonuses on the account', 'retailcrm') . ': ' . $loyaltyAccount['amount'], - __('Bonus card number' , 'retailcrm') . ': ' . ($loyaltyAccount['cardNumber'] ?? __('The card is not linked', 'retailcrm')), - __('Date of registration', 'retailcrm') . ': ' . $loyaltyAccount['activatedAt'], - '
', - '' . __('Current level rules', 'retailcrm') . '', - __('Required amount of purchases to move to the next level', 'retailcrm') . ': ' . $loyaltyAccount['nextLevelSum'], - ]; - - switch ($loyaltyAccount['level']['type']) { - case 'bonus_converting': - $data[] = __('Ordinary goods', 'retailcrm') . ': ' . __('accrual of 1 bonus for each', 'retailcrm') . ' '. $loyaltyAccount['level']['privilegeSize'] . ' ' . $loyaltyAccount['loyalty']['currency']; - $data[] = __('Promotional products', 'retailcrm') . ': ' . __('accrual of 1 bonus for each', 'retailcrm') . ' '. $loyaltyAccount['level']['privilegeSizePromo']. ' ' . $loyaltyAccount['loyalty']['currency']; - break; - case 'bonus_percent': - $data[] = __('Ordinary goods', 'retailcrm') . ': ' . __('bonus accrual in the amount of', 'retailcrm'). ' ' . $loyaltyAccount['level']['privilegeSize'] . '% ' . __('of the purchase amount', 'retailcrm'); - $data[] = __('Promotional products', 'retailcrm') . ': ' . __('bonus accrual in the amount of', 'retailcrm'). ' ' . $loyaltyAccount['level']['privilegeSizePromo'] . '% ' . __('of the purchase amount', 'retailcrm'); - break; - case 'discount': - $data[] = __('Ordinary goods', 'retailcrm') . ': ' . $loyaltyAccount['level']['privilegeSize'] . '% ' . __('discount', 'retailcrm'); - $data[] = __('Promotional products', 'retailcrm') . ': ' . $loyaltyAccount['level']['privilegeSizePromo'] . '% ' . __('discount', 'retailcrm'); - break; - } - - $result = ''; - - foreach ($data as $line) { - $result .= "

$line

"; - } - - return $result; - } } endif; diff --git a/src/include/components/class-wc-retailcrm-loyalty-form.php b/src/include/components/class-wc-retailcrm-loyalty-form.php new file mode 100644 index 0000000..88eafdd --- /dev/null +++ b/src/include/components/class-wc-retailcrm-loyalty-form.php @@ -0,0 +1,90 @@ + +

%s

+

%s%s.

+

%s%s.

+

+

+ + + ', + __('To register in the loyalty program, fill in the form:', 'retailcrm'), + __(' I agree with ', 'retailcrm'), + __('loyalty program terms', 'retailcrm'), + __(' I agree with ', 'retailcrm'), + __('terms of personal data processing', 'retailcrm'), + __('Phone', 'retailcrm'), + __('Send', 'retailcrm'), + __('Close', 'retailcrm') + ); + } + + public function getActivationForm() + { + return sprintf(' +
+

%s

+ +
', + __('Activate participation in the loyalty program', 'retailcrm'), + __('Send', 'retailcrm') + ); + } + + public function getInfoLoyalty(array $loyaltyAccount) + { + $data = [ + '' . __('Bonus account', 'retailcrm') . '', + __('Participation ID: ', 'retailcrm') . $loyaltyAccount['id'], + __('Current level: ', 'retailcrm') . $loyaltyAccount['level']['name'], + __('Bonuses on the account: ', 'retailcrm') . $loyaltyAccount['amount'], + __('Bonus card number: ' , 'retailcrm') . ($loyaltyAccount['cardNumber'] ?? __('The card is not linked', 'retailcrm')), + __('Date of registration: ', 'retailcrm') . $loyaltyAccount['activatedAt'], + '
', + '' . __('Current level rules', 'retailcrm') . '', + __('Required amount of purchases to move to the next level: ', 'retailcrm') . $loyaltyAccount['nextLevelSum'] . ' ' . $loyaltyAccount['loyalty']['currency'] + ]; + + switch ($loyaltyAccount['level']['type']) { + case 'bonus_converting': + $data[] = sprintf(__('Ordinary products: accrual of 1 bonus for each %s %s', 'retailcrm'), $loyaltyAccount['level']['privilegeSize'], $loyaltyAccount['loyalty']['currency']); + $data[] = sprintf(__('Promotional products: accrual of 1 bonus for each %s %s', 'retailcrm'), $loyaltyAccount['level']['privilegeSizePromo'], $loyaltyAccount['loyalty']['currency']); + break; + case 'bonus_percent': + $data[] = sprintf(__('Ordinary products: bonus accrual in the amount of %s% of the purchase amount', 'retailcrm'), $loyaltyAccount['level']['privilegeSize']); + $data[] = sprintf(__('Promotional products: bonus accrual in the amount of %s% of the purchase amount', 'retailcrm'), $loyaltyAccount['level']['privilegeSizePromo']); + break; + case 'discount': + $data[] = sprintf(__('Ordinary products: %s% discount', 'retailcrm'), $loyaltyAccount['level']['privilegeSize']); + $data[] = sprintf(__('Promotional products: %s% discount', 'retailcrm'), $loyaltyAccount['level']['privilegeSizePromo']); + break; + } + + $result = ''; + + foreach ($data as $line) { + $result .= "

$line

"; + } + + return $result; + } + } + + +endif; diff --git a/src/retailcrm.php b/src/retailcrm.php index b3c7de4..2187273 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -132,6 +132,7 @@ if (!class_exists( 'WC_Integration_Retailcrm')) : require_once(self::checkCustomFile('include/validators/url-validator/class-wc-retailcrm-url-constraint.php')); require_once(self::checkCustomFile('include/validators/url-validator/class-wc-retailcrm-url-validator.php')); require_once(self::checkCustomFile('include/validators/class-wc-retailcrm-validator-exception.php')); + require_once(self::checkCustomFile('include/components/class-wc-retailcrm-loyalty-form.php')); } /**