• Resolved bastide.cl

    (@bastidecl)


    Hello,

    I have a simple scheduled export for orders and I would like to add a column to know if a client is a new one or a returning one, as in new_client = 1 or 0.

    I think I could use the order_count or the first order date but I’m not sure which one is the best way to do it, can you help me on that please ?

    Thanks in advance.

    • This topic was modified 3 years ago by bastide.cl.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author algol.plus

    (@algolplus)

    Plugin Author algol.plus

    (@algolplus)

    please, follow to https://docs.algolplus.com/algol_order_export/fields/
    add key “is_first_order”
    use this code

    add_filter('woe_get_order_value_is_first_order',function ($value, $order,$fieldname) {
    	if($order->get_user_id())
    		$total_orders = wc_get_customer_order_count( $order->get_user_id() ) ;
    	else
    		$total_orders = WC_Order_Export_Data_Extractor::get_customer_order_count_by_email( $order->get_billing_email());
    	return ($total_orders == 1 ) ? "Yes" : "No";
    },10,3);
    
    Thread Starter bastide.cl

    (@bastidecl)

    Thank you very much, it works well : )

    Plugin Author algol.plus

    (@algolplus)

    you’re welcome

    Hello there,

    (PRO user here)

    I exactly needed this function and so I did everything you described, works for me as well! But for some reason there are customers who ordered multiple times and yet it still shows “YES” (meaning new customer). In fact, if I use the default “Customer total orders” column in export, I see many cases where the total number is just wrong. I can show you examples if needed.

    Cheers

    Plugin Author algol.plus

    (@algolplus)

    Hello

    please, replace code with this version

    add_filter('woe_get_order_value_is_first_order',function ($value, $order,$fieldname) {
    	$total_orders = WC_Order_Export_Data_Extractor::get_customer_order_count_by_email( $order->get_billing_email());
    	return ($total_orders == 1 ) ? "Yes" : "No";
    },10,3);

    Hello,

    Thank you for your quick response!

    I can see that now it checks the billing email of the user which I think is a better way for this, but unfortunately I still see customers who ordered in the past but in the output it shows them as new customer ??

    Any idea what might cause this?

    Plugin Author algol.plus

    (@algolplus)

    my bad, here is correct version

    add_filter('woe_get_order_value_is_first_order',function ($value, $order,$fieldname) {
    	global $wpdb;
    
    	$total_orders = $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_email'
    			AND     posts.post_type = 'shop_order'
    			AND     posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
    			AND     meta.meta_value = '" . esc_sql( $order->get_billing_email() ) . "'"
    		);
    
    	return ($total_orders == 1 ) ? "Yes" : "No";
    },10,3);

    Hello,

    Thank you! Still the same, but I realised that the ones which are incorrect (indicates as new customer) are guests users who checkout without login, even if they had several orders in the past. Is this the problem you think?

    Plugin Author algol.plus

    (@algolplus)

    Hello

    Do they use same billing email?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to check if this a new customer’ is closed to new replies.