根据免费送货

时间:2018-03-13 21:41:50

标签: php jquery wordpress woocommerce checkout

在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 个答案:

答案 0 :(得分: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文件中。经过测试和工作。