FOX - Currency Switcher for WooCommerce

Comment forcer une devise sur la page de paiement WooCommerce

Il est parfois nécessaire de forcer une devise spécifique sur la page de paiement en fonction de la logique métier.

Méthode la plus simple :

  • Ouvrez le fichier functions.php de votre thème WordPress actuel et ajoutez le code suivant :
  • add_filter('wp_head', function(){    
        if(is_checkout()){
            global $WOOCS;
            $WOOCS->set_currency('USD');
        }
    });
  • Remplacez 'USD' par la devise dont vous avez besoin...
Remarque : Cela fonctionne uniquement lorsque l'option "Is multiple allowed" est activée (Oui).

Méthode avancée :

// Define forced currency once to avoid repetition
define('WOOCS_FORCED_CHECKOUT_CURRENCY', 'USD'); // Change to your currency

// Method 1: Force on page load
add_action('template_redirect', 'woocs_force_checkout_currency', 999);

// Method 2: Force during checkout process (after WOOCS hooks)
add_action('woocommerce_checkout_process', 'woocs_force_checkout_currency', 999);

function woocs_force_checkout_currency() {
    if (!is_checkout()) {
        return;
    }
    
    global $WOOCS;
    
    if (!isset($WOOCS) || !is_object($WOOCS)) {
        return;
    }
    
    $WOOCS->set_currency(WOOCS_FORCED_CHECKOUT_CURRENCY);
    
    // Also set in storage to prevent reset
    if (isset($WOOCS->storage)) {
        $WOOCS->storage->set_val('woocs_current_currency', WOOCS_FORCED_CHECKOUT_CURRENCY);
    }
}

// Method 3: Force on AJAX updates
add_filter('woocommerce_checkout_update_order_review', 'woocs_force_currency_ajax', 10000);

function woocs_force_currency_ajax($post_data) {
    global $WOOCS;
    
    if (!isset($WOOCS) || !is_object($WOOCS)) {
        return $post_data;
    }
    
    $WOOCS->set_currency(WOOCS_FORCED_CHECKOUT_CURRENCY);
    
    if (isset($WOOCS->storage)) {
        $WOOCS->storage->set_val('woocs_current_currency', WOOCS_FORCED_CHECKOUT_CURRENCY);
    }
    
    return $post_data;
}

// Prevent GeoIP override
add_filter('woocs_force_pay_bygeoip_rules', '__return_false');
Source : https://wordpress.org/support/topic/how-to-force-any-currency-on-the-checkout-page/