Php 基于免费送货有条件隐藏电子商务结帐自定义字段
在WooCommerce中,每当选择“免费送货”时,我都试图隐藏一个自定义添加的字段(根据订单金额自动选择) 我想我找到了一个解决方案,下面的代码,但当我在新的(匿名)浏览器窗口加载一个页面时,该字段出现了,如果刷新它,该字段将再次隐藏Php 基于免费送货有条件隐藏电子商务结帐自定义字段,php,jquery,wordpress,woocommerce,checkout,Php,Jquery,Wordpress,Woocommerce,Checkout,在WooCommerce中,每当选择“免费送货”时,我都试图隐藏一个自定义添加的字段(根据订单金额自动选择) 我想我找到了一个解决方案,下面的代码,但当我在新的(匿名)浏览器窗口加载一个页面时,该字段出现了,如果刷新它,该字段将再次隐藏 // Hide address field, when Free Shipping mode is selected add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fi
// Hide address field, when Free Shipping mode is selected
add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');
function xa_remove_billing_checkout_fields($fields) {
$shipping_method ='free_shipping:5'; // Set the desired shipping method to hide the checkout field(s).
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == $shipping_method) {
unset($fields['billing']['billing_field_432']); // Add/change filed name to be hide
}
return $fields;
}
非常感谢您的帮助。由于这是一个实时事件,您需要使用javascript/jQuery使其正常工作。您的“billing_field_432”不必是必需的,因为当字段被隐藏并尝试提交订单时,将为此自定义签出字段抛出一条错误通知消息 根据“免费送货:5”送货方式显示/隐藏该字段的代码:
// Conditional Show hide checkout fields based on chosen shipping methods
add_action( 'wp_footer', 'conditionally_hidding_billing_custom_field' );
function conditionally_hidding_billing_custom_field(){
// Only on checkout page
if( ! is_checkout() ) return;
// HERE your shipping methods rate ID "Free shipping"
$free_shipping = 'free_shipping:5';
?>
<script>
jQuery(function($){
// Choosen shipping method selectors slug
var sm = 'input[name^="shipping_method"]',
smc = sm + ':checked',
cbf = '#billing_field_432_field',
ihc = 'input[name="hidden_check"]';
// Function that shows or hide imput select fields
function showHide( selector = '', action = 'show' ){
if( action == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
$(ihc).val('1'); // Set hidden field for checkout process
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
$(ihc).val(''); // Set hidden field for checkout process
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Initialising: Hide if choosen shipping method is "Free Shipping" method
if( $(smc).val() == '<?php echo $free_shipping; ?>' )
showHide( cbf, 'hide' );
else
$(ihc).val('1'); // Set hidden field for checkout process
// Live event (When shipping method is changed): Show or Hide based on "Free Shipping" method
$( 'form.checkout' ).on( 'change', sm, function() {
if( $(smc).val() == '<?php echo $free_shipping; ?>' )
showHide( cbf, 'hide' );
else
showHide( cbf );
});
});
</script>
<?php
}
此代码位于活动子主题(或主题)的function.php文件中。测试和工作
它基于:
由于您使用的是免费送货方式,最低订购量为“50”和。您应该改为使用此选项:
// Unset checkout field based on cart amount for free shipping
add_filter('woocommerce_checkout_fields', 'remove_checkout_billing_field_432', 999 );
function remove_checkout_billing_field_432( $fields ) {
if ( WC()->cart->cart_contents_total >= 50 ) {
unset($fields['billing']['billing_field_432']); // Unset field
}
return $fields;
}
此代码位于活动子主题(或主题)的function.php文件中。已测试并正常工作。可能的副本
// Unset checkout field based on cart amount for free shipping
add_filter('woocommerce_checkout_fields', 'remove_checkout_billing_field_432', 999 );
function remove_checkout_billing_field_432( $fields ) {
if ( WC()->cart->cart_contents_total >= 50 ) {
unset($fields['billing']['billing_field_432']); // Unset field
}
return $fields;
}