• Resolved amgdre

    (@amgdre)


    Hello!

    Is it possible to show the number of orders per customer? If it’s possible, wich metakey should I use?

    Thank you very much!

    • This topic was modified 7 years, 1 month ago by amgdre.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author algol.plus

    (@algolplus)

    Hello

    Could you try this code ?

    // add field  "orders per customer"
    add_filter('woe_get_order_fields', function ($fields) {
    	$fields['customer_num_orders'] = array( 'label' => 'Total orders per Customer', 'colname' => 'Total orders per Customer', 'checked' => 1 );
    	return $fields;
    });
    // calculate  new field
    add_filter('woe_get_order_value_customer_num_orders', function ($value,$order, $field) {
    	$customer_id = $order->user_id;
    	return $customer_id ? wc_get_customer_order_count($customer_id) : 0 ;
    }, 10, 3);
    
    Plugin Author algol.plus

    (@algolplus)

    baudouin88

    (@baudouin88)

    Hello is it possible to have the same code for the number of orders per company name? (Company)Billing. As I do have multiple order from same companies under different user_id

    Plugin Author algol.plus

    (@algolplus)

    Hello

    yes, but I have no time to do it for free ??

    // add field  "orders per company"
    add_filter('woe_get_order_fields', function ($fields) {
    	$fields['company_num_orders'] = array( 'label' => 'Total orders per Company', 'colname' => 'Total orders per Company', 'checked' => 1 );
    	return $fields;
    });
    // calculate  new field
    add_filter('woe_get_order_value_company_num_orders', function ($value,$order, $field) {
    	global $wpdb;
    	
    	$company = $order->get_billing_company();
    	if( $company ) 
    		$count = $wpdb->get_var( "SELECT COUNT(*)
    			FROM $wpdb->posts as posts
    			LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
    			WHERE   meta.meta_key = '_billing_company'
    			AND     posts.post_type = 'shop_order'
    			AND     posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
    			AND     meta_value = '" . esc_sql( $company ) . "'
    		" );
    	else	
    		$count = 0;
    	return $count;
    }, 10, 3);
    baudouin88

    (@baudouin88)

    Hello. thanks for the reply I really appreciate, I totally understand that you don’t really want to do this for free.

    This code prevent the plugin to perform any export. So it doesn’t work, I’m actually working on it to see what is going on.

    (Also I don’t need it anymore as I was able to export what I wanted, but I’ll totally try to fix and answer my own question later)

    Bests,

    Plugin Author algol.plus

    (@algolplus)

    hi

    if you use Woocommerce before 3.0
    you should replace
    $company = $order->get_billing_company();
    with
    $company = $order->billing_company;

    thanks, Alex

    baudouin88

    (@baudouin88)

    hooooo ok, well I would never thought of that. I tried the update but It didn’t do anything good for me.

    Thanks Alex, keep the good work!

    Bests,
    Baudouin

    Plugin Author algol.plus

    (@algolplus)

    wc_get_order_statuses was added in 3.0 too

    so you can remove this line

    AND posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Total orders per Customer’ is closed to new replies.