diff --git a/src/assets/js/retailcrm-loyalty-actions.js b/src/assets/js/retailcrm-loyalty-actions.js
index c8b626c..c75f08c 100644
--- a/src/assets/js/retailcrm-loyalty-actions.js
+++ b/src/assets/js/retailcrm-loyalty-actions.js
@@ -20,7 +20,10 @@ jQuery(function() {
let phone = jQuery('#phoneLoyalty');
if (!phone.val().match(/(?:\+|\d)[\d\-\(\) ]{7,}\d/)) {
- phone.parent().append('' + messagePhone + '')
+
+ if (!jQuery('#warningLoyaltyPhone').length) {
+ phone.parent().append('' + messagePhone + '')
+ }
event.preventDefault();
return false;
diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php
index 98b013e..b24c4dd 100644
--- a/src/include/class-wc-retailcrm-base.php
+++ b/src/include/class-wc-retailcrm-base.php
@@ -90,8 +90,6 @@ if (!class_exists('WC_Retailcrm_Base')) {
add_action('wp_ajax_upload_selected_orders', [$this, 'upload_selected_orders']);
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);
@@ -106,9 +104,13 @@ if (!class_exists('WC_Retailcrm_Base')) {
add_action('woocommerce_new_order', [$this, 'create_order'], 11, 1);
- add_action('init', [$this, 'add_loyalty_endpoint'], 11, 1);
- add_action('woocommerce_account_menu_items', [$this, 'add_loyalty_item'], 11, 1);
- add_action('woocommerce_account_loyalty_endpoint', [$this, 'show_loyalty'], 11, 1);
+ if (isset($this->settings['loyalty']) && $this->settings['loyalty'] === static::YES) {
+ add_action('wp_ajax_register_customer_loyalty', [$this, 'register_customer_loyalty']);
+ add_action('wp_ajax_activate_customer_loyalty', [$this, 'activate_customer_loyalty']);
+ add_action('init', [$this, 'add_loyalty_endpoint'], 11, 1);
+ add_action('woocommerce_account_menu_items', [$this, 'add_loyalty_item'], 11, 1);
+ add_action('woocommerce_account_loyalty_endpoint', [$this, 'show_loyalty'], 11, 1);
+ }
// Subscribed hooks
add_action('register_form', [$this, 'subscribe_register_form'], 99);
diff --git a/src/include/class-wc-retailcrm-loyalty.php b/src/include/class-wc-retailcrm-loyalty.php
index 4998901..db4d338 100644
--- a/src/include/class-wc-retailcrm-loyalty.php
+++ b/src/include/class-wc-retailcrm-loyalty.php
@@ -51,9 +51,9 @@ if (!class_exists('WC_Retailcrm_Loyalty')) :
return $result;
}
- $loyaltyAccount = $response['loyaltyAccounts'][0];
+ $loyaltyAccount = $response['loyaltyAccounts'][0] ?? null;
- if (isset($response['loyaltyAccounts'][0]) && (int)$loyaltyAccount['customer']['externalId'] === $userId) {
+ if ($loyaltyAccount && (int) $loyaltyAccount['customer']['externalId'] === $userId) {
if ($loyaltyAccount['active'] === true) {
$result['form'] = $this->getLoyaltyInfo($loyaltyAccount);
} else {
diff --git a/tests/datasets/data-loyalty-retailcrm.php b/tests/datasets/data-loyalty-retailcrm.php
new file mode 100644
index 0000000..8c3c5bb
--- /dev/null
+++ b/tests/datasets/data-loyalty-retailcrm.php
@@ -0,0 +1,42 @@
+
+ * @license http://retailcrm.ru Proprietary
+ * @link http://retailcrm.ru
+ * @see http://help.retailcrm.ru
+ */
+
+class DataLoyaltyRetailCrm
+{
+ public static function getDataLoyalty()
+ {
+ return [
+ 'id' => 1,
+ 'level' => [
+ 'name' => 'Test level',
+ 'privilegeSize' => 5,
+ 'privilegeSizePromo' => 3,
+ 'type' => 'bonus_converting'
+ ],
+ 'amount' => 1000,
+ 'cardNumber' => '12345',
+ 'activatedAt' => '2024-04-10 15:00:00',
+ 'nextLevelSum' => 15000,
+ 'loyalty' => [
+ 'currency' => 'USD'
+ ],
+ 'customer' => [
+ 'externalId' => 1
+ ],
+ 'active' => true
+ ];
+ }
+}
diff --git a/tests/helpers/class-wc-retailcrm-test-case-helper.php b/tests/helpers/class-wc-retailcrm-test-case-helper.php
index a099ac2..7f46f85 100644
--- a/tests/helpers/class-wc-retailcrm-test-case-helper.php
+++ b/tests/helpers/class-wc-retailcrm-test-case-helper.php
@@ -80,6 +80,9 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case
'product_description' => 'full',
'stores_for_uploading' => ['woocommerce', 'main'],
'woo_coupon_apply_field' => 'testField',
+ 'loyalty' => 'yes',
+ 'loyalty_terms' => 'Test terms',
+ 'loyalty_personal' => 'Test privacy'
];
update_option(WC_Retailcrm_Base::$option_key, $options);
diff --git a/tests/test-wc-retailcrm-base.php b/tests/test-wc-retailcrm-base.php
index aa280df..46dd14e 100644
--- a/tests/test-wc-retailcrm-base.php
+++ b/tests/test-wc-retailcrm-base.php
@@ -132,6 +132,11 @@ class WC_Retailcrm_Base_Test extends WC_Retailcrm_Test_Case_Helper
$this->assertArrayHasKey('bind_by_sku', $this->baseRetailcrm->form_fields);
$this->assertArrayHasKey('update_number', $this->baseRetailcrm->form_fields);
$this->assertArrayHasKey('product_description', $this->baseRetailcrm->form_fields);
+
+ //loyalty
+ $this->assertArrayHasKey('loyalty', $this->baseRetailcrm->form_fields);
+ $this->assertArrayHasKey('loyalty_terms', $this->baseRetailcrm->form_fields);
+ $this->assertArrayHasKey('loyalty_personal', $this->baseRetailcrm->form_fields);
}
public function test_retailcrm_form_fields_value()
@@ -407,6 +412,18 @@ class WC_Retailcrm_Base_Test extends WC_Retailcrm_Test_Case_Helper
$this->assertEquals('', $this->baseRetailcrm->validate_api_url_field('', 'https://test.simla.com/test'));
}
+ public function test_get_status_coupon()
+ {
+ $result = $this->baseRetailcrm->get_status_coupon();
+ $result = json_decode($result);
+
+ $this->assertEquals('yes', $result['coupon_status']);
+ $this->assertEquals(
+ "To activate the loyalty program it is necessary to activate the 'enable use of coupons option",
+ $result['translate']['coupon_warning']
+ );
+ }
+
private function getJsonData($text)
{
preg_match('/{.*}/', $text, $matches);
diff --git a/tests/test-wc-retailcrm-loyalty.php b/tests/test-wc-retailcrm-loyalty.php
new file mode 100644
index 0000000..0396de5
--- /dev/null
+++ b/tests/test-wc-retailcrm-loyalty.php
@@ -0,0 +1,106 @@
+
+ * @license http://retailcrm.ru Proprietary
+ * @link http://retailcrm.ru
+ * @see http://help.retailcrm.ru
+ */
+class WC_Retailcrm_Loyalty_Test extends WC_Retailcrm_Test_Case_Helper
+{
+ protected $responseMock;
+ protected $apiMock;
+
+ /** @var \WC_Retailcrm_Loyalty */
+ protected $loyalty;
+
+ public function setUp()
+ {
+ $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper')
+ ->disableOriginalConstructor()
+ ->setMethods(['isSuccessful'])
+ ->getMock()
+ ;
+
+ $this->responseMock->setResponse(['success' => true]);
+ $this->setMockResponse($this->responseMock, 'isSuccessful', true);
+
+ $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Client_V5')
+ ->disableOriginalConstructor()
+ ->setMethods(['customersGet', 'getLoyaltyAccountList', 'createLoyaltyAccount', 'activateLoyaltyAccount'])
+ ->getMock()
+ ;
+
+ $this->setMockResponse($this->apiMock, 'customersGet', ['customer' => ['id' => 1]]);
+ $this->setMockResponse($this->apiMock, 'createLoyaltyAccount', $this->responseMock);
+ $this->setMockResponse($this->apiMock, 'activateLoyaltyAccount', $this->responseMock);
+
+ $this->loyalty = new WC_Retailcrm_Loyalty($this->apiMock, []);
+ }
+
+ /**
+ * @dataProvider responseLoyalty
+ */
+ public function testGetForm($isSuccessful, $body, $expected)
+ {
+ $response = new WC_Retailcrm_Response($isSuccessful ? 200 : 400, $body);
+
+ $this->setMockResponse($this->apiMock, 'getLoyaltyAccountList', $response);
+ $this->loyalty = new WC_Retailcrm_Loyalty($this->apiMock, []);
+
+ $result = $this->loyalty->getForm(1);
+
+ if (isset($result['form'])) {
+ $this->assertTrue((bool) stripos($result['form'], $expected));
+ }
+ }
+
+ public function responseLoyalty()
+ {
+ return [
+ [
+ 'isSuccessful' => true,
+ 'body' => json_encode(['loyaltyAccounts' => []]),
+ 'expected' => 'id="loyaltyRegisterForm"'
+ ],
+ [
+ 'isSuccessful' => true,
+ 'body' => json_encode(
+ [
+ 'loyaltyAccounts' => [
+ 0 => [
+ 'active' => false,
+ 'customer' => [
+ 'externalId' => 1
+ ],
+ 'id' => 1
+ ]
+ ]
+ ]
+ ),
+ 'expected' => 'id="loyaltyActivateForm"'
+ ],
+ [
+ 'isSuccessful' => true,
+ 'body' => json_encode(['loyaltyAccounts' => [0 => DataLoyaltyRetailCrm::getDataLoyalty()]]),
+ 'expected' => 'accrual of 1 bonus for each'
+ ]
+ ];
+ }
+}