带有Jquery的WooCommerce函数及其内部PHP

带有Jquery的WooCommerce函数及其内部PHP,php,jquery,wordpress,woocommerce,checkout,Php,Jquery,Wordpress,Woocommerce,Checkout,我希望满足一个条件:小计>500&&客户是fadeIn元素的朋友。代码: function customer_type_js(){ $subtotal = $wc_cart->subtotal; echo "<script type='text/javascript'> jQuery(document).ready(function () { jQuery('#customer_type').on('change'

我希望满足一个条件:小计>500&&客户是fadeIn元素的朋友。代码:

function customer_type_js(){
        $subtotal = $wc_cart->subtotal;
    echo "<script type='text/javascript'>
        jQuery(document).ready(function () {
            jQuery('#customer_type').on('change', function() {
                var customer_type = jQuery('#customer_type').val();
                console.log(customer_type);
                if(customer_type == 'friend'){
                    jQuery('#billing_field').fadeIn();
                    jQuery('.std-checkout-button').fadeOut();
                }
                jQuery('body').trigger('update_checkout');
            });
        });     
        </script>";
}
add_action( 'woocommerce_before_checkout_form', 'customer_type_js');
我尝试了以下设置,但无效:

function customer_type_js(){
        $subtotal = $wc_cart->subtotal;
    echo "<script type='text/javascript'>
        jQuery(document).ready(function () {
            jQuery('#customer_type').on('change', function() {
                var customer_type = jQuery('#customer_type').val();
                console.log(customer_type);
                if(customer_type == 'friend'){
if($subtotal > 500){
                        jQuery('#woocommerce_eu_vat_number').fadeIn();
}
                    jQuery('#billing_field').fadeIn();
                    jQuery('.std-checkout-button').fadeOut();
                }
                jQuery('body').trigger('update_checkout');
            });
        });     
        </script>";
}
add_action( 'woocommerce_before_checkout_form', 'customer_type_js');

首先,移动另一个文件中的所有js。应该只在jQuery'customer\u type'上调用它。在'change'事件上,最好是jQuery'customer\u type'。changefunction{…}

关于钩子-在可以使用jquery listener触发此事件之后,您可以在不使用js的情况下绘制一些东西

这样会更好:

add_action( 'woocommerce_before_checkout_form', function() {
    if (something) {
         if (anotherOne) {
            echo 'jQuery("#anotherEl").fadeIn()';
            return;
         }

         echo 'jQuery("#woocommerce_eu_vat_number").fadeIn()';
    }
});

或显示具有任何id的元素,并尝试触发此操作。此外,您不能在js中使用php代码。在php生成过程中,可以在js中填充一些内容,但这是一个糟糕的解决方案。另外,不要忘记使用return after条件来防止其他操作。

尝试以下挂钩函数,此时您将获得购物车小计$wc\u cart->subtotal在该挂钩中不输出任何内容,因为$wc\u cart参数不存在:

add_action( 'woocommerce_after_order_notes', 'custom_checkout_jquery_script', 30, 1 );
function custom_checkout_jquery_script( $checkout ) {
    $subtotal = (float) WC()->cart->subtotal;
    ?>
    <script type="text/javascript">
        (function($){
            var a = '#customer_type'
                b = <?php echo $subtotal; ?>;
            $(a).on( 'change', function() {
                var c = $(this).val(),
                    d = '#woocommerce_eu_vat_number',
                    e = '#billing_field',
                    f = '.std-checkout-button';
                console.log(c);
                if( c == 'friend' && b >= 500){
                    $(d).fadeIn();
                    $(e).fadeIn();
                    $(f).fadeOut();
                } else {
                    $(d).fadeOut();
                    $(e).fadeOut();
                    $(f).fadeIn();
                }
                $(document.body).trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}
代码进入活动子主题或主题的function.php文件。测试和工作

您还可以尝试将WC->cart->subtotal替换为WC->cart->cart\u内容\u总计,这是不含增值税的非折扣购物车项目小计


所以问题是什么?您使用了双引号,因此$subtotal变量被扩展为实际值。就javascript而言,对于$wc_cart->subtotal值750,它将读取if750>500{,因此这应该可以工作。您是否收到任何错误?谢谢,我将尝试此操作。wc->cart->subtotal是否排除了运费和税?或者我是否需要添加一个-$cart_对象->运费税总额;?
add_action( 'woocommerce_after_order_notes', 'custom_checkout_jquery_script', 30, 1 );
function custom_checkout_jquery_script( $checkout ) {
    $subtotal = (float) WC()->cart->subtotal;
    ?>
    <script type="text/javascript">
        (function($){
            var a = '#customer_type'
                b = <?php echo $subtotal; ?>;
            $(a).on( 'change', function() {
                var c = $(this).val(),
                    d = '#woocommerce_eu_vat_number',
                    e = '#billing_field',
                    f = '.std-checkout-button';
                console.log(c);
                if( c == 'friend' && b >= 500){
                    $(d).fadeIn();
                    $(e).fadeIn();
                    $(f).fadeOut();
                } else {
                    $(d).fadeOut();
                    $(e).fadeOut();
                    $(f).fadeIn();
                }
                $(document.body).trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}