WPIntell

Source evidence

Latest versions destroys page layout

Auto Coupons for WooCommerce · support · 2023-08-12T16:09:00+00:00

mixedsentiment
highseverity
0.94relevance
6replies
Evidence linked to opportunitycommercial context

Proof Health

Open evidence

Commercial opportunities need traceable source links before they are treated as build-worthy.

4 / 26 rows with source links

15.4% of this page's analysis has direct source links.

0 build-decision rows missing links

0 rows here require auditable proof before promotion.

22 rows with no attached evidence

0 rows have source counts but still need direct links.

Conversation

support
Thomas resolved
Hello, In older versions, you were using Woocommerce hooks to execute your function called wac_apply_coupons , which is fine. In new versions, you are hooking this function to the wp_loaded hook, which is not fine. This hook cannot be used to echo. This function is actually echoing things, such as coupon notice even before the theme is loaded. This totally breaks the layout. I had to remove the new hooks and restore those from older versions: /** * Fix woo_auto_coupons echoing on wp_loaded hooks, which completely destroys the page HTML markup * * @return void */ function fix_woo_auto_coupons_hooks() { remove_action('wp_loaded','wac_apply_coupons'); if(function_exists('wac_is_path') && wac_is_path('/cart')) { add_action('woocommerce_add_to_cart','wac_apply_coupons'); // Product add_action('woocommerce_before_checkout_form','wac_apply_coupons'); // Checkout add_action('woocommerce_before_cart','wac_apply_coupons'); // Cart } } add_action('init', 'fix_woo_auto_coupons_hooks'); Would be nice if you could fix that in next versions 🙂 Regards. See above the actual problem in action. All <head> nodes get moved to <body>. Regards. Hi @tlartaud , Thank you for identifying this issue. I appreciate you letting me know about the impact of the wp_loaded hook. In your opinion, would the following fix work? if(isset($_GET['coupon'])) { add_action('init','wac_cache_coupon'); add_action('template_redirect','wac_apply_coupons'); } else { add_action('woocommerce_add_to_cart','wac_apply_coupons'); // Product add_action('woocommerce_before_checkout_form','wac_apply_coupons'); // Checkout add_action('woocommerce_before_cart','wac_apply_coupons'); // Cart } Hello, Thank you for identifying this issue. You’re welcome. In your opinion, would the following fix work? The else part looks definitely okay. About the if(isset($_GET[‘coupon’])) { part, I did not test. I’ll probably do. But I am afraid that the template_redirect hook will also change the expected behavior, and print the notices on unwanted locatons (depending on the theme, this hook might also be executed several times on the page, which should produce multiple notices)…. That’s not really hard to test and confirm. I’ll let you know If I also get such a strange behavior while passing a $_GET parameter. However, the real question is: Is there any specific reason that made you change those hooks? I did not look at the wac_apply_coupons recent changes, but maybe you needed to execute that function earlier? If that’s the case, keep it executed on an early hook, but just send all the echo parts to some others filters. It would prevent the echos to be printed too early. Regards. Hi Thomas, Version 2.2.15 has been released. It now includes output buffering so that all notifications are returned in the content area. This should safely resolve your exact issue without changing when the function is triggered. Please let me know if any reason this doesn’t fix the root issue. Hello, Thank you for the update, this actually fixed the issue. However, I also tried this part (I assume this should automatically apply a coupon while sending a custom URL to a customer like mysite.test/cart/?coupon=COUPON100). if(isset($_GET['coupon'])) { add_action('init','wac_cache_coupon'); add_action('template_redirect','wac_apply_coupons'); } It is not working, and it is actually removing coupons based on number of items added in the cart. Regards. Hi Thomas, Thank you for following up. The coupon parameter will work when the ‘Apply via URL’ setting is checked under the coupon settings. Let me know if this setting is on for the coupon and the URL is still not applying the coupon. The only scenario where it may not apply is if the coupon was manually removed from the cart previously… in this case, apply another coupon then remove it. This resets the coupon cache.

Comments

6 shown
Thomas 2023-08-12T16:18:00+00:00

See above the actual problem in action. All <head> nodes get moved to <body>. Regards.

RLDD 2023-08-12T16:35:00+00:00

Hi @tlartaud , Thank you for identifying this issue. I appreciate you letting me know about the impact of the wp_loaded hook. In your opinion, would the following fix work? if(isset($_GET['coupon'])) { add_action('init','wac_cache_coupon'); add_action('template_redirect','wac_apply_coupons'); } else { add_action('woocommerce_add_to_cart','wac_apply_coupons'); // Product add_action('woocommerce_before_checkout_form','wac_apply_coupons'); // Checkout add_action('woocommerce_before_cart','wac_apply_coupons'); // Cart }

Thomas 2023-08-13T17:38:00+00:00

Hello, Thank you for identifying this issue. You’re welcome. In your opinion, would the following fix work? The else part looks definitely okay. About the if(isset($_GET[‘coupon’])) { part, I did not test. I’ll probably do. But I am afraid that the template_redirect hook will also change the expected behavior, and print the notices on unwanted locatons (depending on the theme, this hook might also be executed several times on the page, which should produce multiple notices)…. That’s not really hard to test and confirm. I’ll let you know If I also get such a strange behavior while passing a $_GET parameter. However, the real question is: Is there any specific reason that made you change those hooks? I did not look at the wac_apply_coupons recent changes, but maybe you needed to execute that function earlier? If that’s the case, keep it executed on an early hook, but just send all the echo parts to some others filters. It would prevent the echos to be printed too early. Regards.

RLDD 2023-08-14T17:21:00+00:00

Hi Thomas, Version 2.2.15 has been released. It now includes output buffering so that all notifications are returned in the content area. This should safely resolve your exact issue without changing when the function is triggered. Please let me know if any reason this doesn’t fix the root issue.

Thomas 2023-08-16T16:32:00+00:00

Hello, Thank you for the update, this actually fixed the issue. However, I also tried this part (I assume this should automatically apply a coupon while sending a custom URL to a customer like mysite.test/cart/?coupon=COUPON100). if(isset($_GET['coupon'])) { add_action('init','wac_cache_coupon'); add_action('template_redirect','wac_apply_coupons'); } It is not working, and it is actually removing coupons based on number of items added in the cart. Regards.

RLDD 2023-08-16T19:47:00+00:00

Hi Thomas, Thank you for following up. The coupon parameter will work when the ‘Apply via URL’ setting is checked under the coupon settings. Let me know if this setting is on for the coupon and the URL is still not applying the coupon. The only scenario where it may not apply is if the coupon was manually removed from the cart previously… in this case, apply another coupon then remove it. This resets the coupon cache.