1
votes

I'm trying to write a simple plugin that would show new or returning customer information and the number of completed orders from the user. To do this, I created a folder called returning-customers.php and file with the same name. In this file, I wrote the next code:

/*
 * Plugin Name: Kasha returning customers
 * Description: Displays information returning customer or new, and also displays the number of completed orders
 * Version: 1.0.0
 * Author: Me
 * License: GPLv2 or later
 */


add_filter( 'woocommerce_admin_order_data_after_order_details', 'render_customer_is_new_field' );

function render_customer_is_new_field($order)
{

    $args = array(
        'status' => 'completed',
        'return' => 'ids',
    );
    
    if ( $order->get_user_id() !== 0 ) {
        $args['customer_id'] = $order->get_customer_id();
    } else {
        $args['customer'] = $order->get_billing_email();
    }
    
    $customer_has_existing_orders  = wc_get_orders( $args );

    $orders_count = count($customer_has_existing_orders);

    $markData = ( (int)count($customer_has_existing_orders) >= 2 )
        ? array('status-processing', 'Returning customer')
        : array('status-on-hold', 'New Customer');


    echo '
    <p class="kasha-customer-counter" style="float: left">
        <mark class="order-status '. $markData[0] .'">
            <span>'. $markData[1] .' <strong style="color:#ff0000;font-weight: 900;"> '. $orders_count .'</strong></span>
        </mark>
    </p>
    ';
}

But this code does not work correctly. I always see that the customer has returned and all his orders are considered. However, the mark that the user is returning customer should be placed only after two orders with the status completed and only orders with the status completed should be considered. Now it looks like the wc_get_orders function is not working. When I write: var_dump(wc_get_orders()) I see NULL.

2

2 Answers

1
votes

First your folder name should be only called returning-customers without .php or any name (in lowercase, without spaces) like woo-returning-customers as it's related to wwocommerce. you can keep your php file name as it's.

Then use the following:

/*
 * Plugin Name: Kasha returning customers
 * Plugin URI:
 * Description: Displays information returning customer or new, and also displays the number of completed orders.
 * Version: 1.0.0
 * Author: Me
 * Author URI:
 * License: GPLv2 or later
 */

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    add_action( 'woocommerce_admin_order_data_after_order_details', 'render_customer_is_new_field' );
    function render_customer_is_new_field( $order ) {
        if( is_a( $order, 'WC_Order') ) {
            $args = array(
                'limit'  => -1,
                'status' => 'completed',
                'return' => 'ids',
            );

            if ( $order->get_user_id() > 0 ) {
                $args['customer_id'] = $order->get_customer_id();
            } else {
                $args['customer'] = $order->get_billing_email();
            }

            $orders_ids   = wc_get_orders( $args );
            $orders_count = intval( count($orders_ids) );
            $markData     = ( $orders_count > 1 ) ? array('status-processing', 'Returning customer') : array('status-on-hold', 'New Customer');

            echo '
            <p class="kasha-customer-counter" style="float: left">
                <mark class="order-status '. $markData[0] .'">
                    <span>'. $markData[1] .' <strong style="color:#ff0000;font-weight: 900;"> '. $orders_count .'</strong></span>
                </mark>
            </p>';
        }
    }
}

Tested and works on last WooCommerce version (and should work on all versions since 3.0).

1
votes

The status should be one of these values: 'wc-pending','wc-processing','wc-on','wc-completed','wc-cancelled','wc-refunded','wc-failed'

Refs: https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#general