问题描述
有人可以帮助我如何修复此代码 不是显示国家而是显示作者 你们能告诉我我在这段代码上做错了什么吗?
add_action( 'woocommerce_after_shop_loop_item_title','sold_by' );
function sold_by(){
?>
<?PHP
global $product;
$seller = get_post_field( 'post_author',$product->get_id());
$author = get_user_by( 'id',$seller );
$store_info = dokan_get_store_info( $author->ID );
$address = dokan_get_store_info( $author->address );
?>
<span class="details">
<?PHP printf( 'Origin: %s',$author->display_name,$address ); ?>
</span>
<?PHP
}
请参阅此屏幕截图,显示作者而不是国家/地区:
解决方法
尝试以下操作:
add_action( 'woocommerce_after_shop_loop_item_title','dokan_store_country' );
function dokan_store_country(){
global $product;
$seller_id = get_post_field( 'post_author',$product->get_id());
$seller = new WP_User( $seller_id );
$countries = WC()->countries->get_countries();
$store_data = dokan_get_store_info( seller_id );
if ( isset( $store_data['country'] ) ) {
$store_country = $store_data['country']; // Try to get seller store country
} elseif( isset( $countries[$seller->billing_country] ) ) {
$store_country = $countries[$seller->billing_country]; // Try to get seller billing country
} else {
return; // Exit
}
echo '<span class="details">' . sprintf( __('Origin: %s'),$store_country ) . '</span>';
}
未经测试,它可以工作。