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

3
在WooCommerce中,我正在尝试隐藏一个自定义添加的字段,每当“免费送货”被选择时(自动选择,基于订单金额)。
我认为下面的代码可以解决问题,但是当我在新的(隐身)浏览器窗口上加载页面时,该字段存在,如果刷新它,则该字段再次隐藏。
// 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;
}

非常感谢您的帮助。


1个回答

3
作为一个实时活动,您需要使用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' 时启用“必填”字段选项检查过程:

// 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文件中。已测试并可运行。
它基于:基于选择的运输方式,在WooCommerce结账页面上有条件地隐藏一个字段
如果您使用的是免费送货方式,最低订单金额为“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文件中。已经进行了测试,能够正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接