【问题标题】:Pass php variable into Wordpress Query function将 php 变量传递给 Wordpress 查询函数
【发布时间】:2021-05-18 16:00:29
【问题描述】:

我想使用 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 jquery wordpress woocommerce


    【解决方案1】:

    您可以使用 PHP json_encode 函数将 PHP 数组转换为 jQuery 对象。

    然后您可以使用jQuery.inArray 来检查 couponCheck 是否存在于 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.');
                }
            });
        });"
    );
    

    代码已经过测试并且可以运行。

    【讨论】:

    • 感谢文森佐!这是完美的。
    猜你喜欢
    • 2019-02-16
    • 2015-12-17
    • 2015-02-19
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    相关资源
    最近更新 更多