Cannot apply filters from inside a function in wooCommerce for ajax posted data

My Ajax data is handled by the function my_currencySwitcher and I’m storing the ajax value in a global variable $postedValue and I’m applying filters and once in the filter I’m trying to assign the $currency to $postedValue but the filter doesn’t work if I hardcode $currency to’EUR’ it works. How to assign it dynamically, I mean how can I directly use $postedValue without hardcoding it and how to make this filter only specific to a particular user?

<?php
/**
* Customizing the adminbar with a currency switcher
*
*/
//require_once ‘woocommerce-functions.php’;
$postedValue;
function my_currencySwitcher(){
global $postedValue;
$postedValue= $_POST[‘valueSelec’];
apply_filters(‘woocommerce_currency_symbol’);

echo json_encode(“currency changed successfully”);

die();
exit;

}
add_action(‘wp_ajax_nopriv_hookto_adminbar’, ‘my_currencySwitcher’); // regular website visitor
add_action(‘wp_ajax_hookto_adminbar’, ‘my_currencySwitcher’); // admin user

function my_symbol(){
global $postedValue;
$currency= $postedValue; //if i hardcode this it works
switch( $currency ) {
case ‘EUR’:
$currency_symbol = ‘€’;
break;
case ‘GBP’:
$currency_symbol = ‘£’;
break;
default:
// USD and CAD
$currency_symbol = ‘$’;
break;
}

// ARRAY (
// [0] => $
// [1] => USD
// )
return $currency_symbol;
}

add_filter( ‘woocommerce_currency_symbol’, ‘my_symbol’, 10, 2 );

Read more here:: Cannot apply filters from inside a function in wooCommerce for ajax posted data

Leave a Reply

Your email address will not be published. Required fields are marked *