WooCommerce-结帐时禁用地址字段

时间:2019-01-10 17:35:33

标签: woocommerce

对于虚拟类型的产品,我正在使用以下功能隐藏结帐中的地址字段。

我添加了此功能,以防止系统对此类型的产品收取运费。

add_filter( 'woocommerce_checkout_fields' , 'simple_virtual_checkout', 20, 1 );
function simple_virtual_checkout( $fields ) {
    $only_virtual_products = true;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check for non-virtual products
        if ( ! $cart_item['data']->is_virtual() ) 
            $only_virtual_products = false; 
    }

    if( $only_virtual_products ) {
        // remove billing fields
        unset($fields['billing']['billing_company']);
        unset($fields['billing']['billing_address_1']);
        unset($fields['billing']['billing_address_2']);
        unset($fields['billing']['billing_city']);
        unset($fields['billing']['billing_postcode']);
        unset($fields['billing']['billing_country']);
        unset($fields['billing']['billing_state']);
        unset($fields['billing']['billing_neighborhood']);
        unset($fields['billing']['billing_number']);
        unset($fields['billing']['billing_cellphone']);

        add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');

        // remove shipping fields 
        unset($fields['shipping']['shipping_first_name']);    
        unset($fields['shipping']['shipping_last_name']);  
        unset($fields['shipping']['shipping_company']);
        unset($fields['shipping']['shipping_address_1']);
        unset($fields['shipping']['shipping_address_2']);
        unset($fields['shipping']['shipping_city']);
        unset($fields['shipping']['shipping_postcode']);
        unset($fields['shipping']['shipping_country']);
        unset($fields['shipping']['shipping_state']);
        unset($fields['shipping']['shipping_neighborhood']);
        unset($fields['shipping']['shipping_number']);

    }
    return $fields;
}

它正常工作,因为没有地址,因此不会进行运费计算。

我遇到的问题是,当用户登录时已经注册了地址。

在这种情况下,有什么方法可以实现此功能,而不是不从注册表中提取此数据?地址,城市,邮政编码等...

0 个答案:

没有答案