首页 文章

使用woocommerce删除选定的结帐字段,具体取决于送货方式

提问于
浏览
1

您好,我正在尝试根据所选的送货方式,找出如何使用woocommerce checkout删除一些结算字段 . 因此,使用此代码,当客户选择本地送货时,我会尝试取消设置帐单地址,结算城市,结算状态和结算邮政编码,但此代码无效 . 任何帮助,将不胜感激 .

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

1 回答

  • 0

    以下是我将如何解决这个问题 .
    这将涉及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;
    }
    

    我们不会取消设置字段,而是改变它的 required ness . 这意味着,如果所选方法是我们要检查的方法,我们将不会要求它 . 然后我们将添加一个 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;
    }
    

相关问题