Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - Add custom meta column to WooCommerce product listing

We want to add the vendor store name to each product in a column. I know how how to add a column, to get the vendor store name but I have still an empty array when I output the vendor for each product.

Someone able to help out? :)

add_filter( 'manage_edit-product_columns', 'show_product_order',15 );
function show_product_order($columns){

   //add column
   $columns['vendor_store_name'] = __( 'Vendor'); 

   return $columns;
}

function get_dokan_vendor_shop_name_from_product_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 );
    if ( ! empty( $store_info['store_name'] ) ) {
        return $vendor->get_shop_name();
    } else {
        return;
    }
}

add_action( 'manage_product_posts_custom_column', 'vendor_product_column', 10, 2 );

function vendor_product_column( $column, $postid ) {
    if ( $column == 'vendor_store_name' ) {
        echo get_post_meta( $postid, $store_info, true );
    }
}

UPDATE

I'm now trying to output the info directly in the loop where I get the vendor. But still an empty array. It this approach actually possible to get it done?

add_filter( 'manage_edit-product_columns', 'show_product_order',15 );
function show_product_order($columns){

   //add column
   $columns['vendor_store_name'] = __( 'Vendor'); 

   return $columns;
}

function vendor_product_column( $product_id ) {
   foreach  ( $products->get_meta as $product ) {
    
    $product = $item->get_product();
    $author_id = $product->post->post_author;
    $vendor = dokan()->vendor->get( $author_id );
    $shop_name = $vendor->get_shop_name();
       
    }
    
     if ( $column == 'vendor_store_name' ) {
        echo $store_info;
    }
}

add_action( 'manage_product_posts_custom_column', 'vendor_product_column', 10, 2 );

question from:https://stackoverflow.com/questions/65944020/add-custom-meta-column-to-woocommerce-product-listing

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

1 Answer

0 votes
by (71.8m points)

I was able to fix it! Here is the code :)

add_filter( 'manage_edit-product_columns', 'custom_admin_products_store_name_column', 9999 );
 
function custom_admin_products_store_name_column( $columns ){
   $columns['vendor_store_name'] = __( 'Vendor'); 
   return $columns;
}
 
add_action( 'manage_product_posts_custom_column', 'custom_admin_products_store_name_column_content', 10, 2 );
 
function custom_admin_products_store_name_column_content( $column, $product_id ){

    $seller = get_post_field( 'post_author', $product_id);
    $store_info = dokan_get_store_info( $seller );
    $store_name = $store_info['store_name'];

    if ( $column == 'vendor_store_name' ) {
        echo __($store_name);
        
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...