Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

wordpress - WooCommerce show vendor store-name (Dokan) in admin order details overview

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.

  • If only items from one vendor is in the order => result: => Vendor(s): Vendor A
  • If items from different vendors are in the order => result: => Vendor(s): Vendor A, Vendor B, Vendor 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;
    }
}


UPDATE

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.

enter image description here

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 );
question from:https://stackoverflow.com/questions/65901474/woocommerce-show-vendor-store-name-dokan-in-admin-order-details-overview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...