在WooCommerce结帐时显示客户帐单地址和帐单城市,而不是“发货”标签

时间:2020-07-06 15:20:11

标签: woocommerce

通过使用woocommerce_shipping_package_name过滤器,我希望修改标签文本,以在客户登录后包含帐单邮寄地址和帐单城市。

这是最终结果的示例:

要发送至:

大街1

纽约

(中间没有多余的行)

我以为我已经准备好了一切,但是我遇到了一个错误。

Object of class WP_User could not be converted to int

我了解客户可以注册,然后将产品添加到购物车中,然后去结帐,如果可以,则不会有帐单地址或帐单城市。因此,在继续显示文本之前,我真的需要检查元数据是否为空(上面的预期输出)。

允许,我是新来的,所以..在这里寻求帮助。

到目前为止,我的代码:

add_filter( 'woocommerce_shipping_package_name', 'customer_address_city_with_shipping' );
function customer_address_city_with_shipping( $name ) {

    // only for logged in customers
    if (is_user_logged_in()){
    
    // get the current customer
    $user_id = wp_get_current_user();

    // make sure it's a valid customer ID
    if ($user_id == 0) return;

    // customer billing address and city, how to check if it's empty or not?
    $customer_address = get_user_meta( $user_id, 'shipping_address_1', true );
    $customer_city = get_user_meta( $user_id, 'shipping_city', true );

    // if logged in and if there's a billing address and billing city, let's go!
    return 'To be delivered to:<br /><span style="font-weight:normal!important;">'.$customer_address.'<br />'.$customer_city.'';

    } else {
    // if not.. then don't

    return 'Shipping &amp; Delivery';
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码中有一些错误。请尝试以下操作:

add_filter( 'woocommerce_shipping_package_name', 'customer_address_city_with_shipping' );
function customer_address_city_with_shipping( $name ) {
    // only for logged in customers
    if ( is_user_logged_in() && $user_id = get_current_user_id() ) {
        // Get customer shipping address and city
        $address = get_user_meta( $user_id, 'shipping_address_1', true );
        $city    = get_user_meta( $user_id, 'shipping_city', true );
        
        if( !empty($address) && !empty($city) ) {
            // Shipping address and city are not empty
            return __('To be delivered to:') . '<br /><span style="font-weight:normal !important;">' 
            . $address . '<br />' . $city . '</span>';
        } else {
            // Shipping address and city are not defined
            return 'Shipping &amp; Delivery';
        }
    } else {
        // User is not logged in
        return 'Shipping &amp; Delivery';
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中。应该可以。