phpwordpresswoocommerce

WooCommerce Token-Based Discount Works Until Adding to Cart on Internet Explorer


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:

The Problem:

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:

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?


Solution

  • 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).