I am trying to use wp_localize_script()
to send some php values to a script.
The following are the part of the file inc/show-event.php
.
if( $price ) {
$pricefortwo = ceil( ( 2 * $price) - ( 0.2 * $price ) );
$savefortwo = ( 2 * $price) - $pricefortwo;
$priceforthree = ceil( ( 3 * $price) - ( 0.333 * $price ) );
$saveforthree = ( 3 * $price) - $priceforthree;
$priceforretake = ceil( 0.5 * $price );
$saveforretake = $price - $priceforretake;
// Setting the session variables
$_SESSION['price'] = $price;
$_SESSION['price_for_two'] = $pricefortwo;
$_SESSION['price_for_three'] = $priceforthree;
$_SESSION['price_for_retake'] = $priceforretake;
$session_array = array(
'price' => $_SESSION['price'],
'price_for_two' => $_SESSION['price_for_two'],
'price_for_three' => $_SESSION['price_for_three'],
'price_for_retake' => $_SESSION['price_for_retake']
);
wp_localize_script( 'init_show_calendar', 'session_param', $session_array );
}
After this when I try and use the object name session_param
in init_show_calendar.js
file it throws a js error saying session_param
is not defined. but when i use the following code in functions.php
.
$session_array = array(
'price' => $_SESSION['price'],
'price_for_two' => $_SESSION['price_for_two'],
'price_for_three' => $_SESSION['price_for_three'],
'price_for_retake' => $_SESSION['price_for_retake']
);
wp_localize_script( 'init_show_calendar', 'session_param', $session_array );
it returns me the variable but it doesn't return the latest values, it returns the value stored in a page refresh.
FYI: The show-event.php
is called in a plugin in the following way
add_action('wp_ajax_get_event', array($this, 'render_frontend_modal'));
function render_frontend_modal() {
require_once AEC_PATH . 'inc/show-event.php';
}
Please let me know if you want any more information.
json_encode
your$session_array
and return that from your ajax handler.