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
307 views
in Technique[技术] by (71.8m points)

php - Filter products by clickable value in custom column WooCommerce

We add a new column to display the author => store-name form each product. (Add custom meta column to WooCommerce product listing)

I was able to answer the mentioned question by my self. But now we ran into a new problem. The needed information are now in the new column, but we can not click it. We want to have the entries in that new column clickable in order that every product from that author would be filtered. So it's a question about how to make a clickable link in order to show all other product from that author.

This is our 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);
        
    }
}

I already tried around with the information from here: WooCommerce: Adding a custom filter to the Product Admin area

But this is more for a new filter than a clickable value in a column which will apply a filter.

question from:https://stackoverflow.com/questions/65951591/filter-products-by-clickable-value-in-custom-column-woocommerce

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

1 Answer

0 votes
by (71.8m points)

I was able to get this to work! :)

Here is my code. Is this a proper solution or where can I improve this? :)

Thanlks

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'];
    
$vendor_products = get_admin_url() . 'edit.php?post_type=product&author=' . $seller;

if ( $column == 'vendor_store_name' ) {
        printf('<a href="%s">%s</a>', $vendor_products, $store_name);       
}
    

}

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

...