【问题标题】:Show Dokan vendor country on WooCommerce product catalog在 WooCommerce 产品目录上显示 Dokan 供应商国家
【发布时间】:2021-01-30 17:16:50
【问题描述】:

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

        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 
    
    
    }

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

【问题讨论】:

    标签: php wordpress woocommerce country dokan


    【解决方案1】:

    尝试以下方法:

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

    未经测试,它可以工作。

    【讨论】:

    • 我怎样才能把这个放在产品类别先生之后?
    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2020-11-26
    相关资源
    最近更新 更多