gpt4 book ai didi

php - 将 php 变量传递给 Wordpress 查询函数

转载 作者:行者123 更新时间:2023-12-04 07:38:57 25 4
gpt4 key购买 nike

我想使用 ACF 存储一个变量,然后在 wc_enqueue_js 标签中使用该变量。如果该变量可通过 php 作为 $couponCheck 使用,我需要将其作为 'updated_checkout' 函数内的数组提供。所以当优惠券被提交时,它会被数组检查。
这有意义吗?

<?php $couponCheck = get_field('coupons');>
wc_enqueue_js( "jQuery(function($) {
// On coupon change
$(document.body).on('updated_checkout', function(){
var couponArray = XXX;
var couponCheck = $('.woocommerce-remove-coupon').attr('data-coupon');
if(jQuery.inArray('couponCheck', coupnArray)) {
$('.gift-with-coupon').show();
}
});
});");
?>

谢谢。

最佳答案

您可以使用 PHP json_encode函数将 PHP 数组转换为 jQuery 对象。
那么你可以使用jQuery.inArray检查是否 优惠券查询 存在于 Javascript 对象中。

$couponCheck = get_field('coupons');
wc_enqueue_js(
"jQuery(function($) {
// On coupon change
$(document.body).on('updated_checkout', function(){
var couponArray = " . json_encode( $couponCheck ) . ";
var couponCheck = $('.woocommerce-remove-coupon').attr('data-coupon');
if(jQuery.inArray('couponCheck', couponArray)) {
$('.gift-with-coupon').show();
alert('Funziona.');
}
});
});"
);
该代码已经过测试并且可以正常工作。

关于php - 将 php 变量传递给 Wordpress 查询函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67589886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com