在 WooCommerce 产品目录上显示 Dokan 供应商国家

问题描述

有人可以帮助我如何修复此代码 不是显示国家而是显示作者 你们能告诉我我在这代码上做错了什么吗?

        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 
    
    
    }

请参阅此屏幕截图,显示作者而不是国家/地区:

see this photo showing origin as author instead of country

解决方法

尝试以下操作:

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>';
}

未经测试,它可以工作。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...