wordpress - 根据运输方式使用 woocommerce 删除选定的结帐字段

标签 wordpress woocommerce

你好,所以我想弄清楚如何根据所选的运输方式使用 woocommerce 结账删除一些账单字段。因此,当客户选择本地送货时,我试图使用此代码取消设置账单地址、账单城市、账单状态和账单邮政编码,但此代码不起作用。任何帮助,将不胜感激。

add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');

function xa_remove_billing_checkout_fields($fields) {
$shipping_method ='local_pickup:1'; // 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_address_1']); // Add/change filed name to be hide
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_state']);
    unset($fields['billing']['billing_postcode']);
}
return $fields;
}

最佳答案

下面是我将如何解决这个问题。
这将涉及 php、css 和 javascript (jQuery)。

PHP

add_filter( 'woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields' );
function xa_remove_billing_checkout_fields( $fields ) {
    // change below for the method
    $shipping_method ='local_pickup:1'; 
    // change below for the list of fields
    $hide_fields = array( 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode' );

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    // uncomment below line and reload checkout page to check current $chosen_methods
    // print_r($chosen_methods);
    $chosen_shipping = $chosen_methods[0];

    foreach($hide_fields as $field ) {
        if ($chosen_shipping == $shipping_method) {
            $fields['billing'][$field]['required'] = false;
            $fields['billing'][$field]['class'][] = 'hide';
        }
        $fields['billing'][$field]['class'][] = 'billing-dynamic';
    }

    return $fields;
}

我们不会取消设置字段,而只是改变它的必需性。 这意味着,如果选择的方法是我们要检查的方法,我们将不会要求它。然后我们将添加一个hide 类。有了这个,我们可以使用 css 隐藏这些字段。而且 woocommerce 不会抛出它是必需的错误。使用 jQuery,我们可以显示/隐藏这些字段。因此,如果我们在第一次运行时取消设置,则不会显示任何内容,因为字段一开始就不存在,因此页面需要重新加载。

所以这是 javascript 和 css 部分。

add_action( 'wp_footer', 'cart_update_script', 999 );
function cart_update_script() {
    if (is_checkout()) :
    ?>
    <style>
        .hide {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {

            // woocommerce_params is required to continue, ensure the object exists
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }

            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
                // change local_pickup:1 accordingly 
                $('.billing-dynamic').toggleClass('hide', this.value == 'local_pickup:1');
            });
        });
    </script>
    <?php
    endif;
}

关于wordpress - 根据运输方式使用 woocommerce 删除选定的结帐字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50498391/

相关文章:

php - 如何在我的过滤器 Hook 中获取订单对象

excel - 使用 Excel/VB 根据父产品列表创建产品变体列表

javascript - Woocommerce 下单按钮脚本

css - 免费 wordpress.com 网站上的全 Angular 页面

mysql - ORDER BY 查询非常慢

wordpress - 我正在使用 TCPDF 在我的 wordpress 插件中生成 pdf,但在执行时它在已发送的库 header 上有错误。

php - 尝试在 woocommerce 中触发 webhook 后不发送电子邮件的原因跟踪错误

php - 店面产品页面获取产品摘要并向右浮动以匹配图像对齐

wordpress - WordPress 操作、过滤器和 Hook 存储在哪里?

javascript - 滑动 jQuery 面板在 iPad 上不起作用