【问题标题】:Conditionally hide Woocommerce checkout custom field based on free shipping基于免费送货有条件地隐藏 Woocommerce 结帐自定义字段
【发布时间】:2018-03-13 21:41:50
【问题描述】:

在Woocommerce中,只要选择“免费交付”,我正在尝试隐藏一个自定义添加的字段(根据订单量自动选择)。

我在想我找到了一个带有以下代码的解决方案,但是当我在新的 (incognito) 浏览器窗口上加载页面时,该字段存在,如果刷新它,该字段将再次隐藏.

// 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;
}

感谢任何帮助。

【问题讨论】:

标签: php jquery wordpress woocommerce checkout


【解决方案1】:

因为它是一个现场活动,你需要使用 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 文件中。经过测试并且可以工作。

它基于:Conditionally hide a Checkout field in WooCommerce based on chosen shipping


由于您使用的是最低订购量为“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 文件中。经过测试并且可以工作。

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 2020-11-05
    • 1970-01-01
    • 2015-08-14
    • 2015-04-09
    • 2016-04-22
    • 2021-08-13
    • 1970-01-01
    • 2020-06-22
    相关资源
    最近更新 更多