我正在尝试使用wp_localize_script()
将一些php值发送到脚本。
以下是文件的一部分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 );
}
在此之后,当我尝试使用对象名称时session_param
在里面init_show_calendar.js
它抛出一个js错误session_param
未定义。但是当我在中使用以下代码时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 );
它返回变量,但不返回最新的值,它返回存储在页面刷新中的值。仅供参考:Theshow-event.php
在插件中按以下方式调用
add_action(\'wp_ajax_get_event\', array($this, \'render_frontend_modal\'));
function render_frontend_modal() {
require_once AEC_PATH . \'inc/show-event.php\';
}
如果你想了解更多信息,请告诉我。