• Resolved s10dulkar

    (@s10dulkar)


    is there a way to add column in back end of products for sales count so that admin can see which products sold how many times ?

Viewing 15 replies - 1 through 15 (of 16 total)
  • Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi, @s10dulkar!

    Yes, the following code should add a “Total sales” column to the products:

    add_filter( 'manage_edit-product_columns', 'misha_total_sales_1', 20 );
    
    add_action( 'manage_posts_custom_column', 'misha_total_sales_2' );
    
    add_filter('manage_edit-product_sortable_columns', 'misha_total_sales_3');
    
    add_action( 'pre_get_posts', 'misha_total_sales_4' );
     
    function misha_total_sales_1( $col_th ) {
     
    	return wp_parse_args( array( 'total_sales' => 'Total Sales' ), $col_th );
    }
     
    function misha_total_sales_2( $column_id ) {
     
    	if( $column_id  == 'total_sales' )
    		echo get_post_meta( get_the_ID(), 'total_sales', true );
    }
     
    function misha_total_sales_3( $a ){
    	return wp_parse_args( array( 'total_sales' => 'by_total_sales' ), $a );
    }
     
    function misha_total_sales_4( $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;
     
    }

    Also see: https://rynaldostoltz.com/how-to-add-custom-code-to-your-woocommerce-wordpress-site-the-right-way/

    Cheers!

    Thread Starter s10dulkar

    (@s10dulkar)

    Thank you for a very quick reply sir. Do I need to add this code to function.php ?

    Thread Starter s10dulkar

    (@s10dulkar)

    And I want to show it in backend only not frontend

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi, @s10dulkar!

    Yes, to your theme’s functions.php file or via a plugin that allows custom code to be added, such as https://www.ads-software.com/plugins/code-snippets/

    Also, yes, it will only show in products backend, not on the front-end.

    Cheers!

    Thread Starter s10dulkar

    (@s10dulkar)

    Thank you very much sir. Do I need to change anything in code or copy paste exactly it is ? Sorry I am not that tech savvy.

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi, @s10dulkar!

    No changes needed.

    Thread Starter s10dulkar

    (@s10dulkar)

    sir its giving error as below,

    Your PHP code changes were rolled back due to an error on line 45 of file wp-content/themes/flatsome-child-theme/functions.php. Please fix and try saving again.
    
    syntax error, unexpected 'function' (T_FUNCTION)

    45th line is this
    function misha_total_sales_1( $col_th ) {

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi, @s10dulkar!

    What version PHP are you using?

    Thread Starter s10dulkar

    (@s10dulkar)

    how can i find that sir ?

    Thread Starter s10dulkar

    (@s10dulkar)

    5.6.30

    Thread Starter s10dulkar

    (@s10dulkar)

    5.6.39 on another site. this one i tried on site with 5.6.30

    Thread Starter s10dulkar

    (@s10dulkar)

    @rynald0s i have two sites where i checked that site have 5.6.30 and another site i have is 5.6.39

    @s10dulkar
    Did you use the code snippets plugin or functions.php?

    Thread Starter s10dulkar

    (@s10dulkar)

    Functions.php

    functions.php of your main theme is not a good place because any edits will be overwritten by theme updates.

    functions.php in a child theme would be good.

    If there is some other code in functions.php above this new code, does it end with php enabled? If it ends with ?>, php is switched off before it gets to the additional lines, so re activate it with the php opening tag <?php.

    Check the syntax in the whole of functions.php by copying and pasting its code in this validation service:
    https://phpcodechecker.com/

    In the event of it still not working, please copy and paste functions.php here:
    https://pastebin.com/
    (free service) and paste just the link in this thread so someone can take a look at it.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘is there a way to add column in backend of products for sales count’ is closed to new replies.