Skip to content

Commit 3f82f22

Browse files
bug fix and feature update for logs (#97)
* bug fix and feature update for logs added a toggle for logging. this way we don't saturate the logs and we have fine control on the process. changed the logic around checking for store ID and isolate the check for storeID instead of having 2 checks. Also fixed a decimal rounding issue. * Update class-helloextend-protection-logger.php added an initialize function to make sure the settings is never undefined. * Update class-helloextend-global.php added log_enabled and debug_log_enabled to the compact function * Update class-helloextend-global.php removed unused variable * Update class-helloextend-global.php removed a circular reference * Refactored frontend test to use in-stock product --------- Co-authored-by: Alex Smith <alex.smith@extend.com>
1 parent 6c56798 commit 3f82f22

6 files changed

Lines changed: 109 additions & 49 deletions

File tree

cypress/integration/02_frontend_checks.cy.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
const URLS = {
2+
EXTEND_API_OFFER: 'https://api.helloextend.com/offers*',
3+
WOO_ADD_TO_CART: 'https://woocommerce.woodys.extend.com/?wc-ajax=add_to_cart',
4+
WOODYS: 'https://woocommerce.woodys.extend.com/',
5+
WOODYS_PRODUCT: 'https://woocommerce.woodys.extend.com/product/air-purifier/',
6+
}
7+
18
describe('Frontend Page and API Validation', () => {
29
it('Checks Homepage for 500 Errors', () => {
3-
cy.visit('https://woocommerce.woodys.extend.com/', { failOnStatusCode: false });
10+
cy.visit(URLS.WOODYS, { failOnStatusCode: false });
411

512
// Ensure the page loaded without 500 errors
613
cy.document().then((doc) => {
@@ -9,7 +16,7 @@ describe('Frontend Page and API Validation', () => {
916
});
1017

1118
it('Checks Product Page for 500 Errors', () => {
12-
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/', { failOnStatusCode: false });
19+
cy.visit(URLS.WOODYS_PRODUCT, { failOnStatusCode: false });
1320

1421
// Ensure the page loaded without 500 errors
1522
cy.document().then((doc) => {
@@ -18,10 +25,10 @@ describe('Frontend Page and API Validation', () => {
1825
});
1926

2027
it('Intercepts Offers API Request and Validates Query Parameters', () => {
21-
cy.intercept('GET', 'https://api.helloextend.com/offers*').as('offersRequest');
28+
cy.intercept('GET', URLS.EXTEND_API_OFFER).as('offersRequest');
2229

2330
// Visit the product page
24-
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
31+
cy.visit(URLS.WOODYS_PRODUCT);
2532

2633
// Wait for the intercepted API request
2734
cy.wait('@offersRequest').then((interception) => {
@@ -34,20 +41,20 @@ describe('Frontend Page and API Validation', () => {
3441

3542
// Validate expected query parameters
3643
expect(queryParams.storeId).to.equal('0e03bcd1-8a00-4a2a-bf2d-7c4d336c07e9');
37-
expect(queryParams.productId).to.equal('104');
44+
expect(queryParams.productId).to.equal('15');
3845
expect(queryParams.category).to.equal('Electronics');
39-
expect(queryParams.price).to.equal('69900');
46+
expect(queryParams.price).to.equal('22900');
4047
} else {
4148
throw new Error('API request was not intercepted.');
4249
}
4350
});
4451
});
4552

4653
it('Intercepts Offers API Response Checks for Plans In ADH or Base', () => {
47-
cy.intercept('GET', 'https://api.helloextend.com/offers*').as('offersResponse');
54+
cy.intercept('GET', URLS.EXTEND_API_OFFER).as('offersResponse');
4855

4956
// Visit the product page
50-
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
57+
cy.visit(URLS.WOODYS_PRODUCT);
5158

5259
// Wait for the API response
5360
cy.wait('@offersResponse').then((interception) => {
@@ -76,7 +83,7 @@ describe('Frontend Page and API Validation', () => {
7683

7784
it('Validates Extend PDP Offers & Modal Are Working', () => {
7885
// Visit product page
79-
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
86+
cy.visit(URLS.WOODYS_PRODUCT);
8087

8188
// Check if the Extend Offer iFrame exists
8289
cy.get('div.helloextend-offer > div.extend-product-offer > iframe')
@@ -135,11 +142,11 @@ describe('Frontend Page and API Validation', () => {
135142
});
136143

137144
it('Ensures Cart Normalization and Simple Offers Are working', () => {
138-
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
145+
cy.visit(URLS.WOODYS_PRODUCT);
139146

140147
cy.get('[name="add-to-cart"]').then(($button) => {
141148
let productDetails = {
142-
productId: '104'// Check for different attributes
149+
productId: '15'// Check for different attributes
143150
// ... other product details you need ...
144151
};
145152

@@ -151,9 +158,9 @@ describe('Frontend Page and API Validation', () => {
151158
// the UI and directly interacts with WooCommerce's backend.
152159
cy.request({
153160
method: 'POST',
154-
url: 'https://woocommerce.woodys.extend.com/?wc-ajax=add_to_cart', // Adjust if needed
161+
url: URLS.WOO_ADD_TO_CART, // Adjust if needed
155162
body: {
156-
product_id: '104', // Use the extracted product ID
163+
product_id: '15', // Use the extracted product ID
157164
quantity: 1, // Or whatever quantity you want
158165
// ... any other required parameters (variation IDs, etc.) ...
159166
},
@@ -222,15 +229,15 @@ describe('Frontend Page and API Validation', () => {
222229
});
223230

224231
it('Validates Checkout Flow and Shipping Protection', () => {
225-
cy.visit('https://woocommerce.woodys.extend.com/product/15in-dell-laptop/');
232+
cy.visit(URLS.WOODYS_PRODUCT);
226233

227234
// 1. Get Product Details (Important!)
228235
// You'll need to extract the product ID and any other relevant info
229236
// to properly add it to the cart via the API. Inspect the page
230237
// to see how WooCommerce structures its product data. Here's an example:
231238
cy.get('[name="add-to-cart"]').then(($button) => {
232239
let productDetails = {
233-
productId: '104'// Check for different attributes
240+
productId: '15'// Check for different attributes
234241
// ... other product details you need ...
235242
};
236243

@@ -242,9 +249,9 @@ describe('Frontend Page and API Validation', () => {
242249
// the UI and directly interacts with WooCommerce's backend.
243250
cy.request({
244251
method: 'POST',
245-
url: 'https://woocommerce.woodys.extend.com/?wc-ajax=add_to_cart', // Adjust if needed
252+
url: URLS.WOO_ADD_TO_CART, // Adjust if needed
246253
body: {
247-
product_id: '104', // Use the extracted product ID
254+
product_id: '15', // Use the extracted product ID
248255
quantity: 1, // Or whatever quantity you want
249256
// ... any other required parameters (variation IDs, etc.) ...
250257
},

helloextend-protection/admin/class-helloextend-protection-admin.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ public function enqueue_scripts()
161161
$nonce = wp_create_nonce('helloextend_sync_nonce');
162162
$helloextend_sync_batch = $this->helloextend_protection_for_woocommerce_settings_catalog_sync_options['helloextend_sync_batch'];
163163
$debug_log_enabled = $this->helloextend_protection_for_woocommerce_settings_general_options['enable_helloextend_debug'];
164+
$log_enabled = $this->helloextend_protection_for_woocommerce_settings_general_options['enable_helloextend_log'];
164165

165-
wp_enqueue_script('helloextend_script');
166+
167+
wp_enqueue_script('helloextend_script');
166168
wp_enqueue_script('helloextend_sync_script');
167-
wp_localize_script('helloextend_sync_script', 'ExtendWooCommerce', compact('store_id', 'ajaxurl', 'environment', 'nonce', 'helloextend_sync_batch', 'debug_log_enabled'));
169+
wp_localize_script('helloextend_sync_script', 'ExtendWooCommerce', compact('store_id', 'ajaxurl', 'environment', 'nonce', 'helloextend_sync_batch', 'debug_log_enabled', 'log_enabled'));
168170

169171
/* end for sync */
170172
global $current_screen;
@@ -480,6 +482,14 @@ public function helloextend_protection_for_woocommerce_settings_page_init()
480482
'helloextend_setting_environment_section' // section
481483
);
482484

485+
add_settings_field(
486+
'enable_helloextend_log', // id
487+
'Enable General Log', // title
488+
array($this, 'enable_helloextend_log_callback'), // callback
489+
'helloextend-protection-for-woocommerce-settings-admin-general', // page
490+
'helloextend_setting_environment_section' // section
491+
);
492+
483493
add_settings_field(
484494
'enable_helloextend_debug', // id
485495
'Enable Debugging Log', // title
@@ -568,6 +578,7 @@ public function helloextend_protection_for_woocommerce_settings_page_init()
568578
if (get_option('helloextend_protection_for_woocommerce_general_settings') == null) {
569579
$settings = [
570580
'enable_helloextend_debug' => '0',
581+
'enable_helloextend_log' => '0',
571582
'helloextend_environment' => 'sandbox',
572583
'helloextend_sandbox_store_id' => '',
573584
'helloextend_live_store_id' => '',
@@ -682,6 +693,10 @@ public function helloextend_protection_for_woocommerce_settings_sanitize($input)
682693
$sanitary_values['enable_helloextend_debug'] = $input['enable_helloextend_debug'];
683694
}
684695

696+
if (isset($input['enable_helloextend_log'])) {
697+
$sanitary_values['enable_helloextend_log'] = $input['enable_helloextend_log'];
698+
}
699+
685700
if (isset($input['helloextend_enable_cart_offers'])) {
686701
$sanitary_values['helloextend_enable_cart_offers'] = $input['helloextend_enable_cart_offers'];
687702
}
@@ -1225,6 +1240,15 @@ public function enable_helloextend_debug_callback()
12251240
);
12261241
}
12271242

1243+
public function enable_helloextend_log_callback()
1244+
{
1245+
printf(
1246+
'<input type="checkbox" name="helloextend_protection_for_woocommerce_general_settings[enable_helloextend_log]" id="enable_helloextend_log" value="1" %s>',
1247+
(isset($this->helloextend_protection_for_woocommerce_settings_general_options['enable_helloextend_log'])
1248+
&& $this->helloextend_protection_for_woocommerce_settings_general_options['enable_helloextend_log'] === '1') ? 'checked' : ''
1249+
);
1250+
}
1251+
12281252
public function helloextend_use_special_price_callback()
12291253
{
12301254
printf(

helloextend-protection/includes/class-helloextend-global.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ public static function helloextend_get_settings()
186186
$settings['enable_helloextend_debug'] = array_key_exists('enable_helloextend_debug', $helloextend_protection_general_settings)
187187
? $helloextend_protection_general_settings['enable_helloextend_debug'] : 0;
188188

189+
$settings['enable_helloextend_log'] = array_key_exists('enable_helloextend_log', $helloextend_protection_general_settings)
190+
? $helloextend_protection_general_settings['enable_helloextend_log'] : 0;
191+
189192
/* shipping protection */
190193
if ($helloextend_protection_shipping_protection_settings) {
191194
$settings['enable_helloextend_sp'] = array_key_exists('enable_helloextend_sp', $helloextend_protection_shipping_protection_settings)
@@ -252,9 +255,11 @@ public static function helloextend_get_settings()
252255
$settings['warranty_product_id'] = array_key_exists('warranty_product_id', $settings)
253256
? $settings['warranty_product_id'] : helloextend_product_protection_id();
254257

255-
if (empty($settings['warranty_product_id'])) {
258+
/* circular reference removed.
259+
if (empty($settings['warranty_product_id'])) {
256260
HelloExtend_Protection_Logger::helloextend_log_error('Error: Warranty product is not created.');
257261
}
262+
*/
258263

259264
return $settings;
260265
}
@@ -404,29 +409,25 @@ public function helloextend_init_global()
404409
$environment = ($environment == 'live') ? $environment : 'demo';
405410
$helloextend_enabled = array_key_exists('enable_helloextend', $settings) ? $settings['enable_helloextend'] : 0;
406411
$ajaxurl = admin_url('admin-ajax.php');
407-
408-
if ($store_id && ($helloextend_enabled === '1')) {
409-
wp_enqueue_script('helloextend_script');
410-
wp_enqueue_script('helloextend_global_script');
411-
wp_localize_script('helloextend_global_script', 'ExtendWooCommerce', compact('store_id', 'ajaxurl', 'environment'));
412-
413-
// Get the leadToken from URL parameters
414-
$lead_token = $this->get_lead_token_from_url();
415-
if ($lead_token) {
416-
// Sanitize the token for safe JavaScript output
417-
$safe_lead_token = esc_js($lead_token);
418-
419-
// Output JavaScript to console
420-
echo "<script type='text/javascript'>\n";
421-
echo "console.log('found leadToken: ', '" . $safe_lead_token . "');\n";
422-
echo "</script>\n";
423-
424-
// next step: Run Post Purchase logic to handle lead Token
425-
$this->helloextend_post_purchase($lead_token, $store_id, $environment, $ajaxurl);
426-
}
427-
} else {
428-
HelloExtend_Protection_Logger::helloextend_log_error('Store Id missing or Extend Product Protection is disabled');
429-
}
412+
$debug_log_enabled = array_key_exists('enable_helloextend_debug', $settings) ? $settings['enable_helloextend_debug'] : 0;
413+
$log_enabled = array_key_exists('enable_helloextend_log', $settings) ? $settings['enable_helloextend_log'] : 0;
414+
415+
if ($store_id){
416+
if ($helloextend_enabled === '1') {
417+
wp_enqueue_script('helloextend_script');
418+
wp_enqueue_script('helloextend_global_script');
419+
wp_localize_script('helloextend_global_script', 'ExtendWooCommerce', compact('store_id', 'ajaxurl', 'environment', 'debug_log_enabled', 'log_enabled'));
420+
421+
// Get the leadToken from URL parameters
422+
$lead_token = $this->get_lead_token_from_url();
423+
if ($lead_token) {
424+
// next step: Run Post Purchase logic to handle lead Token
425+
$this->helloextend_post_purchase($lead_token, $store_id, $environment, $ajaxurl);
426+
}
427+
}
428+
} else {
429+
HelloExtend_Protection_Logger::helloextend_log_error('Store Id is missing');
430+
}
430431
}
431432

432433
/*

helloextend-protection/includes/class-helloextend-protection-logger.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@
1313
exit;
1414
}
1515

16+
1617
class HelloExtend_Protection_Logger
1718
{
19+
private static ?array $settings = null;
1820

21+
private static function helloextend_log_initialize(): void {
22+
if (self::$settings === null) {
23+
/* retrieve environment variables */
24+
self::$settings = HelloExtend_Protection_Global::helloextend_get_settings();
25+
}
26+
}
27+
1928
public static function helloextend_log_error( $message )
2029
{
21-
30+
self::helloextend_log_initialize();
31+
if (self::$settings['enable_helloextend_log'] == 0) {
32+
return;
33+
}
2234
/* Get error logs from the wp_options table... */
2335
$error_log = get_option('helloextend_error_log');
2436

@@ -63,7 +75,10 @@ public static function helloextend_log_error( $message )
6375

6476
public static function helloextend_log_notice( $message )
6577
{
66-
78+
self::helloextend_log_initialize();
79+
if (self::$settings['enable_helloextend_log'] == 0) {
80+
return;
81+
}
6782
/* Get notice logs from the wp_options table... */
6883
$notice_log = get_option('helloextend_notice_log', true);
6984
if (! $notice_log ) {
@@ -110,6 +125,13 @@ public static function helloextend_log_notice( $message )
110125

111126
public static function helloextend_log_debug( $message )
112127
{
128+
self::helloextend_log_initialize();
129+
if (self::$settings['enable_helloextend_log'] == 0) {
130+
return;
131+
}
132+
if (self::$settings['enable_helloextend_debug'] == 0) {
133+
return;
134+
}
113135

114136
/* Get debug logs from the wp_options table... */
115137
$debug_log = get_option('helloextend_debug_log', true);
@@ -563,6 +585,11 @@ public static function helloextend_logger_add_to_new_logs( $id, $type )
563585

564586
public static function helloextend_logger_ajax_call()
565587
{
588+
self::helloextend_log_initialize();
589+
if (self::$settings['enable_helloextend_log'] == 0) {
590+
return;
591+
}
592+
566593
$method = isset($_POST['method']) ? sanitize_text_field(wp_unslash($_POST['method'])) : null;
567594
$message = isset($_POST['message']) ? sanitize_text_field(wp_unslash($_POST['message'])) : null;
568595

@@ -591,4 +618,4 @@ public static function helloextend_logger_ajax_call()
591618
add_action('wp_ajax_helloextend_logger_delete_single', array( $helloextendProtectionLogger, 'helloextend_logger_delete_single' ));
592619

593620
add_action('wp_ajax_nopriv_helloextend_logger_ajax_call', array( $helloextendProtectionLogger, 'helloextend_logger_ajax_call' ), 10);
594-
add_action('wp_ajax_helloextend_logger_ajax_call', array( $helloextendProtectionLogger, 'helloextend_logger_ajax_call' ), 10);
621+
add_action('wp_ajax_helloextend_logger_ajax_call', array( $helloextendProtectionLogger, 'helloextend_logger_ajax_call' ), 10);

helloextend-protection/includes/class-helloextend-protection-orders.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private function is_item_helloextend_lead($item)
156156

157157
private function get_price_in_cents($item_price)
158158
{
159-
return (int) floatval($item_price * 100);
159+
return (int) round((float) $item_price * 100);
160160
}
161161

162162
private function get_purchased_leads($order)

helloextend-protection/js/helloextend-global.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
if(!ExtendWooCommerce) { return;
55
}
66

7-
const { store_id: storeId, ajaxurl, environment, debug_log_enabled: debugLogEnabled } = ExtendWooCommerce;
7+
const { store_id: storeId, ajaxurl, environment, debug_log_enabled: debugLogEnabled, log_enabled: logEnabled } = ExtendWooCommerce;
88

99
Extend.config({
1010
storeId,
@@ -91,7 +91,8 @@
9191
getCart,
9292
warrantyAlreadyInCart,
9393
extendAjaxLog,
94-
debugLogEnabled
94+
debugLogEnabled,
95+
logEnabled
9596
}
9697

9798
$(document).trigger('integration.extend');

0 commit comments

Comments
 (0)