Home > Enterprise >  Add total orders and customer status in new order email notification in WooCommerce
Add total orders and customer status in new order email notification in WooCommerce

Time:09-12

I would like to add a note to the new order email notifications that the admin gets. This note should indicate whether the order is from a new customer, or from a returning customer.

This so that the people in the warehouse know what they should add in the shippingbox. We give new customers a ticket and don't want customers who order again to receive the same ticket, they get a diffrent ticket.

This should apply to both logged in users and users who are not logged in. A check via email address perhaps?

This code is using the has_bought for all customer orders - source

function has_bought() {
    // Get all customer orders
    $customer_orders = get_posts( array(
        'numberposts' => 1, // one order is enough
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed', // Only orders with "completed" status
        'fields'      => 'ids', // Return Ids "completed"
    ) );

    // return "true" when customer has already at least one order (false if not)
   return count($customer_orders) > 0 ? true : false; 
}

But I don't get it on the the new order email that admins receive, I would like to show it as:

  • Customer: new OR returning
  • Number of purchases: 4

How would i go about this and is this possible to create?

CodePudding user response:

Some comments/suggestions:

  • To display text in email notifications, you can use different hooks, depending on where exactly you want to display the text. For example, the woocommerce_email_order_details hook is very suitable for this
  • To target a specific email notification you can use $email->id
  • Via wc_get_orders() you can get and count the existing orders for a customer based on the email address. If desired, pass a series of arguments that define the criteria for the search

So you get:

function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    // Target specific email notification
    if ( $email->id == 'new_order' ) {
        // Get email
        $customer_email = $order->get_billing_email();

        // NOT empty
        if ( ! empty ( $customer_email ) ) {
            // Get orders from customer by email and statuses
            $orders_by_customer_email = wc_get_orders( array(
                'customer'  => $customer_email,
                'status'    => array( 'wc-on-hold','wc-processing','wc-completed' ),
                'limit'     => -1,
                'return'    => 'ids'
            ));

            // When new customer
            if ( count( $orders_by_customer_email ) == 1 ) {
                $customer = __( 'new', 'woocommerce' );
            } else {
                $customer = __( 'returning', 'woocommerce' );
            }

            // Output
            echo '<p style="color:red;font-size:14px;">' . sprintf( __( 'Customer: %s, number of purchases: %d', 'woocommerce' ), $customer, count( $orders_by_customer_email ) ) . '</p>';
        }
    }
}
add_action( 'woocommerce_email_order_details', 'action_woocommerce_email_order_details', 10, 4 );
  • Related