I'm trying to implement a token-based discount system on my WooCommerce store. It works perfectly on most browsers (Chrome, Firefox, Edge) but I'm facing issues on Internet Explorer (IE). The discount is correctly applied when the token is in the URL, and products show the discounted prices on the shop and product pages. However, once I add the product to the cart, it reverts to the regular price.
What I’m trying to achieve:
If the user visits the shop with the URL parameter ?token=AX4HN, I set a cookie to store the discount token for an hour.
The token-based discount should persist as the user navigates through the site and adds products to the cart.
The Problem:
On Internet Explorer, the discount is applied correctly when I first visit a product page using the token, but when I click "Add to Cart", the price reverts back to the regular price.
This issue is only happening in Internet Explorer. Other browsers work as expected.
Step 2: Set a cookie if the token is present in the URL
function set_token_cookie() {
if (isset($_GET['token']) && $_GET['token'] === 'AX4HN') {
setcookie('discount_token', 'AX4HN', time() + 3600, '/'); Cookie lasts for 1 hour
$_COOKIE['discount_token'] = 'AX4HN'; // Set it immediately for current request
}
}
add_action('init', 'set_token_cookie');
Step 3: Check if the token is valid
function has_valid_token() {
return (isset($_GET['token']) && $_GET['token'] === 'AX4HN') ||
(isset($_COOKIE['discount_token']) && $_COOKIE['discount_token'] === 'AX4HN');
}
Apply discount if token is valid
add_filter('woocommerce_product_get_price', 'custom_price_discount', 20, 2);
function custom_price_discount($price, $product) {
$discount_rate = 0.8; // 20% discount
if (has_valid_token() && !$product->is_on_sale()) {
return floatval($price) * $discount_rate;
}
return $price;
}
Observations:
The discounted price displays correctly on the product page in IE.
Once I click "Add to Cart", the cart page shows the regular price instead of the discounted price.
It seems like Internet Explorer is not reading the cookie properly after navigating away from the initial page.
How can I ensure that the discount remains consistent in Internet Explorer even after adding products to the cart? Is there a better way to handle token-based discounts across the site that is compatible with Internet Explorer?
You should better use a WC Session variable for that, instead of capricious cookies, like:
// Set a custom token discount in a WC Session variable (with an expiration time)
add_action( 'woocommerce_init', 'manage_wc_session_token_discount' );
function manage_wc_session_token_discount(){
if( is_admin() ) {
return;
}
// Force enabling WC Session for unlogged users
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
// Set the token discount as a WC Session variable (with 1 hour as expiration time)
if ( ! WC()->session->__isset('discount_token') && isset($_GET['token']) && $_GET['token'] === 'AX4HN' ) {
// Set current timestamp value + 1 hour (expiration time)
WC()->session->set('discount_token', time() + HOUR_IN_SECONDS );
}
// Remove the token discount after expiration time
elseif ( WC()->session->__isset('discount_token') && WC()->session->get('discount_token') <= time() ) {
WC()->session->__unset('discount_token');
}
}
// Conditional fuction: Check for a valid discount token
function has_valid_discount_token() {
return ( isset($_GET['token']) && $_GET['token'] === 'AX4HN') || WC()->session->__isset('discount_token');
}
// Add conditionally a custom discount to product prices globaly
add_filter('woocommerce_product_get_price', 'custom_price_discount', 20, 2);
function custom_price_discount($price, $product) {
if ( has_valid_discount_token() && !$product->is_on_sale()) {
return floatval($price) * 0.8; // 20% discount
}
return $price;
}
It should better work (untested).