【发布时间】:2018-03-22 22:00:24
【问题描述】:
我想满足一个条件:小计 > 500 && 客户是朋友来淡入一个元素。代码:
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');
条件:如果选择了 frind,则显示 billing_field。但我还想要另一个条件。 如果客户类型是朋友 && $subtotal >500 显示 woocommerce_eu_vat
jQuery('#woocommerce_eu_vat_number').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'){
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');
【问题讨论】:
-
那么...问题是什么?您使用的是双引号,因此
$subtotal变量被扩展为实际值。就 javascript 而言,这将读取if(750 > 500) {的$wc_cart->subtotal值 750,所以这应该可以工作。你有任何错误吗?
标签: php jquery wordpress woocommerce checkout