基于免费送货有条件地隐藏 Woocommerce 结帐自定义字段

Conditionally hide Woocommerce checkout custom field based on free shipping

在 WooCommerce 中,我试图在选择 "free delivery" 时隐藏一个自定义添加的字段(自动选择,基于订单金额)。

我以为我找到了下面代码的解决方案,但是当我在新的 (incognito) 浏览器 window 上加载页面时,该字段存在并且如果刷新它,该字段将再次隐藏。

// 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" 不必是必需的,因为当字段被隐藏并尝试提交订单时,将为此自定义结帐字段抛出错误通知消息。

根据 'free_shipping: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
}

添加自定义结帐隐藏输入字段以在 'billing_field_432' 未隐藏时启用 "required" 字段选项检查过程的代码:

// Add a hidden input field for "required" option check process
add_filter('woocommerce_checkout_fields', 'add_custom_hidden_input_field' );
function add_custom_hidden_input_field( $fields ) {

    echo '<input type="hidden" id="hidden_check" name="hidden_check" value="">';

    return $fields;
}

// Check custom checkout field "billing_field_432" when not hidden
add_action('woocommerce_checkout_process', 'check_custom_field_checkout_process');
function check_custom_field_checkout_process() {
    // Check if set, if its not set add an error.
    if ( isset( $_POST['hidden_check'] ) && $_POST['hidden_check'] && empty( $_POST['billing_field_432'] ) )
        wc_add_notice( __( 'Please enter something in "billing field 432".' ), 'error' ); // SET your custom error notice
}

此代码在您的活动子主题(或主题)的 function.php 文件中。已测试并有效。

它基于:


由于您使用的是免费送货方式,最低订购量为“50”且hiding other shipping methods when "free shipping" is available。你应该改用这个:

// 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 文件中。已测试并有效。