Search code examples
phpwordpresswoocommercecountrydokan

Show Dokan vendor country on WooCommerce product catalog


Can someone help me how can i fix this code instead of showing the country it shows the author can you guys tell me what i did wrong on this code?

        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 screenshot showing origin as author instead of country:

see this photo showing origin as author instead of country


Solution

  • Try the following :

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

    Untested, it could work.