noscript-img

Woocommerce Gesamtzahl der Verkäufe anzeigen in der Produkt Übersicht

add_filter( 'manage_edit-product_columns', 'bdev_add_total_sales_to_product_list', 20 );
add_action( 'manage_posts_custom_column', 'bdev_display_total_sales_in_product_list' );
add_filter('manage_edit-product_sortable_columns', 'bdev_sort_total_sales_product_list');
add_action( 'pre_get_posts', 'bdev_total_sales_query_product_list' );
 
function bdev_add_total_sales_to_product_list( $col_th ) {
    return wp_parse_args( array( 'total_sales' => 'Total Sales' ), $col_th );
}
 
function bdev_display_total_sales_in_product_list( $column_id ) {
    if( $column_id  == 'total_sales' )
        echo get_post_meta( get_the_ID(), 'total_sales', true );
}
 
function bdev_sort_total_sales_product_list( $a ){
    return wp_parse_args( array( 'total_sales' => 'by_total_sales' ), $a );
}
 
function bdev_total_sales_query_product_list( $query ) {
    if( !is_admin() || empty( $_GET['orderby']) || empty( $_GET['order'] ) )
        return;
 
    if( $_GET['orderby'] == 'by_total_sales' ) {
        $query->set('meta_key', 'total_sales' );
        $query->set('orderby', 'meta_value_num');
        $query->set('order', $_GET['order'] );
    }
 
    return $query;
 
}