Untitled

 avatar
unknown
plain_text
5 months ago
2.5 kB
5
Indexable

add_filter('option_woocommerce_tax_display_shop', function($tax_display) {
    if (isset($_COOKIE['wc_tax_display']) && in_array($_COOKIE['wc_tax_display'], ['incl', 'excl'])) {
        return $_COOKIE['wc_tax_display'];
    }
    return $tax_display;
}, 10000000, 1);

function add_tax_switcher_script() {
    ?>
    <style>
        .tax-display-switcher {
            padding: 12px 24px;
            background-color: #ffffff;
            color: #2c3338;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            font-weight: 500;
            text-transform: uppercase;
            font-size: 14px;
            letter-spacing: 0.5px;
        }

        .tax-display-switcher:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            background-color: #f8f9fa;
        }
    </style>
    <script>
    function setCookie(name, value, days) {
        let expires = "";
        if (days) {
            const date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function switchTaxDisplay(button) {
        const currentDisplay = button.getAttribute('data-current');
        const newDisplay = currentDisplay === 'incl' ? 'excl' : 'incl';
        
        setCookie('wc_tax_display', newDisplay, 30);
        
        // Update button text
        button.textContent = newDisplay === 'incl' ? 'Prices include tax' : 'Prices exclude tax';
        button.setAttribute('data-current', newDisplay);
        
        // Reload page to update prices
        window.location.reload();
    }
    </script>
    <?php

    echo do_shortcode('[tax_display_switcher]');
}
add_action('wp_footer', 'add_tax_switcher_script');

function tax_display_switcher_shortcode() {
    $current_display = isset($_COOKIE['wc_tax_display']) ? $_COOKIE['wc_tax_display'] : 'incl';
    $button_text = $current_display === 'incl' ? 'Prices include tax' : 'Prices exclude tax';
    
    return sprintf(
        '<button onclick="switchTaxDisplay(this)" data-current="%s" class="tax-display-switcher">%s</button>',
        esc_attr($current_display),
        esc_html($button_text)
    );
}
add_shortcode('tax_display_switcher', 'tax_display_switcher_shortcode');
Editor is loading...
Leave a Comment