Woocommerce 显示客户帐单地址和帐单城市,而不是“开”;航运;电子商务结帐单上的标签
通过使用Woocommerce 显示客户帐单地址和帐单城市,而不是“开”;航运;电子商务结帐单上的标签,woocommerce,Woocommerce,通过使用woocommerce\u shipping\u package\u name过滤器,我希望修改标签文本,以便在客户登录时包含账单地址和账单城市 这是最终结果的一个例子: 发送至: add_filter( 'woocommerce_shipping_package_name', 'customer_address_city_with_shipping' ); function customer_address_city_with_shipping( $name ) { // o
woocommerce\u shipping\u package\u name
过滤器,我希望修改标签文本,以便在客户登录时包含账单地址和账单城市
这是最终结果的一个例子:
发送至:
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 & Delivery';
}
}
大街1号
纽约
(中间没有多余的行)
我以为我把一切都准备好了,但我犯了个错误
Object of class WP_User could not be converted to int
我知道客户可以注册,然后将产品添加到购物车中,然后去结账,如果是这样的话,就不会有账单地址或账单城市。因此,在继续显示文本(上面的预期输出)之前,我确实需要检查meta是否为空
当然,我对这方面还是新手。。在这里寻求帮助
到目前为止我的代码:
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 & Delivery';
}
}
add_filter('woocommerce_shipping_package_name'、'customer_address_city_with_shipping');
功能客户地址城市配送($name){
//仅适用于已登录的客户
如果(用户是否已登录){
//获取当前客户
$user\u id=wp\u get\u current\u user();
//确保它是有效的客户ID
如果($user\u id==0)返回;
//客户账单地址和城市,如何检查是否为空?
$customer\u address=get\u user\u meta($user\u id,'shipping\u address\u 1',true);
$customer\u city=get\u user\u meta($user\u id,'shipping\u city',true);
//如果登录了,如果有账单地址和账单城市,我们走吧!
返回“发送至:
”。$customer\u地址。“
”。$customer\u城市。”;
}否则{
//如果没有,那就不要
返回“装运和交付”;
}
}
您的代码中有一些错误。请尝试以下操作:
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 & Delivery';
}
} else {
// User is not logged in
return 'Shipping & Delivery';
}
}
add_filter('woocommerce_shipping_package_name'、'customer_address_city_with_shipping');
功能客户地址城市配送($name){
//仅适用于已登录的客户
如果(用户是否已登录()&&$user\u id=get\u current\u user\u id()){
//获取客户配送地址和城市
$address=get_user_meta($user_id,'shipping_address_1',true);
$city=get_user_meta($user_id,'shipping_city',true);
如果(!empty($address)&&!empty($city)){
//配送地址和城市不为空
返回uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
.$address.
.$city;
}否则{
//未定义配送地址和城市
返回“装运和交付”;
}
}否则{
//用户未登录
返回“装运和交付”;
}
}
代码进入活动子主题(或活动主题)的functions.php文件。应该能用。啊,好的。非常感谢。我没有完全错:)谢谢你纠正它。