php - 根据免费送货有条件地隐藏 Woocommerce 结账自定义字段

标签 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;
}

感谢任何帮助。

最佳答案

由于这是一个现场事件,您需要使用 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 文件中。经过测试并有效。

关于php - 根据免费送货有条件地隐藏 Woocommerce 结账自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49266306/

相关文章:

php - 注册的自定义帖子类型保存在 WordPress 数据库的哪个位置?

WordPress 将所有 HTTPS 重定向到 HTTP

php 和 mysql 主键

jquery - HTML 表格行链接

php - 使用MySQL改进算法

jquery - 在 jquery 中使用隐藏和显示时不需要的 "flickering"效果

javascript - 用于测试日期格式的正则表达式

php - 获取数组最高值查询mysql,php

php - 在 Behat/Mink FeatureContext 中使用 Laravel Eloquent

php - 如何在 Lithium PHP 框架的查询中使用 SUM 聚合函数