WooCommerce在管理员订单详细信息概述中显示供应商商店名称(Dokan) [英] WooCommerce show vendor store-name (Dokan) in admin order details overview

查看:54
本文介绍了WooCommerce在管理员订单详细信息概述中显示供应商商店名称(Dokan)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想在管理订单详细信息概述中显示订单中的供应商.我们使用以下代码中的某些部分来显示发票中每种产品的供应商.现在,我们要显示实际在订单中的供应商以进行管理员概述.

We want to show the vendor(s) from a order in the Admin order detail overview. We use some parts from the code below to display the vendor for each product in the invoice. Now we want to display which vendor are in the order actually for admin overview.

  • 如果只有一个供应商的商品处于订单中,则>结果:=>供应商:供应商A
  • 如果来自不同供应商的商品按顺序排列=>结果:=>供应商:供应商A,供应商B,供应商C

这是我们目前所拥有的:

This is what we have so far:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'get_dokan_vendor_shop_name_from_order_test', 10, 1 );


function get_dokan_vendor_shop_name_from_order_test( $product_id ) {
    if( empty($product_id) ) return;
    $seller = get_post_field( 'post_author', $product_id );
    $author = get_user_by( 'id', $seller );
    $vendor = dokan()->vendor->get( $seller );
    $store_info = dokan_get_store_info( $author->ID );
    
    echo '<h4>' . __('TEST 1 - Vendor in order') . ' (' . $vendor->get_shop_name() . ')</h4>';

    
    if ( ! empty( $store_info['store_name'] ) ) {
        return $vendor->get_shop_name();
        
    echo '<h4>' . __('TEST 2 - Vendor in order') . ' (' . $vendor->get_shop_name() . ')</h4>';

        
    } else {
        return;
    }
}



更新

到目前为止,我们有了新信息:显示供应商,但是如果订单中有2个来自供应商A的商品,那么它将显示3个供应商A.

With the new information, this is what we have so far: It displays the vendor but if a order has 2 items from vendor A, then it displays Vendor A three times.

因此,我们现在在输出方面遇到了问题.供应商订单信息现在可用,但是输出不是我们想要的方式.

So we now just have problems with the output. The vendor order infos are now available but the output is not the way we want that.

function action_woocommerce_admin_order_data_after_billing_address( $order ) {
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Get product object
        $product = $item->get_product();
        
        // Seller
        $seller = $product->post->post_author;
        
        // Author
        $author = get_user_by( 'id', $seller );
        
        // Store info
        $store_info = dokan_get_store_info( $author->ID );
        
        // Vendor
        $vendor = dokan()->vendor->get( $seller );
        
        // Output Vendor in order - TEST 1
        echo '<h4>' . __('TEST 2 - Vendor in order') . ' (' . $vendor->get_shop_name() . ')</h4>';

    }
        // Output Vendor in order - TEST 2
        echo '<h4>' . __('TEST 2 - Vendor in order') . ' (' . $vendor->get_shop_name() . ')</h4>';

    
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );

推荐答案

我不使用dokan插件,但是 woocommerce_admin_order_data_after_billing_address 挂钩包含 order 对象变量,而不是 $ product_id .

I don't use the dokan plugin, but the woocommerce_admin_order_data_after_billing_address hook contains the order object as passed variable, not the $product_id.

因此,您可以通过订单对象等循环获取商品.我相信这足够了

So you can obtain the items by loop through the order object, etc.. I believe this should suffice

function action_woocommerce_admin_order_data_after_billing_address( $order ) {
    // Empty array
    $shop_names = array();

    // Output
    echo '<h4>' . __( 'Vendor in order: ', 'woocommerce' ) . '</h4>';
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Get product object
        $product = $item->get_product();
        
        // Author id
        $author_id = $product->post->post_author;
        
        // Shopname
        $vendor = dokan()->vendor->get( $author_id );
        $shop_name = $vendor->get_shop_name();
        
        // OR JUST USE THIS FOR SHOPNAME
        // Shop name
        // $shop_name = dokan()->vendor->get( $author_id )->get_shop_name();
        
        // NOT in array
        if ( ! in_array( $shop_name, $shop_names ) ) {
            // Push to array
            $shop_names[] = $shop_name;

            // Output
            echo '<p>' . $shop_name . '</p>';
        }
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );

这篇关于WooCommerce在管理员订单详细信息概述中显示供应商商店名称(Dokan)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆