如果已经完成 WooCommerce,则隐藏地址

Hide address if already completed WooCommerce

我正在寻找一种方法来隐藏我的 woocommerce 主题的结帐页面上的帐单地址,如果用户已经填写了帐单表格(来自以前的订单,或者如果用户之前已经从"my account" 页)。

如果用户已登录,我已经找到了在结帐页面上完全隐藏账单/送货单的方法(见下文),但是我找不到执行上述操作的方法。

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    if( is_user_logged_in() ){
        unset($fields['billing']);
        $fields['billing'] = array();
    }
    return $fields;
}

有什么想法吗?

谢谢!

这将取决于您认为什么是完整的地址,我制作了您可以用来进一步处理的代码片段功能。

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    if( is_user_logged_in() && !has_billing()){
        unset($fields['billing']);
        $fields['billing'] = array();
    }
    return $fields;
}

// Check the meta of Postcode and Country if they are entered.
function has_billing($user_id = false){
            if(!$user_id)
                $user_id = get_current_user_id();

            $shipping_postcode = get_user_meta( $user_id, 'billing_postcode', true );
            $shipping_country = get_user_meta( $user_id, 'billing_country', true );

            // Fetch more meta for the condition has needed.  
            if($shipping_postcode && $shipping_country){
                return true;
            }

            return false;
}

注意:前缀 shipping_ 有一个用于计费( billing_ )。

编辑:这里的元键 billing_address_1billing_address_2 总是前缀可以是 billing_shipping_

此外,如果由于某种原因您没有在用户元键中关联的送货或账单地址,但客户曾经下过订单,您可以检查此代码以获取订单地址。

Woocommerce WC_Order get_shipping_address() not returning as array (old post might not be valid anymore)