Search code examples
wordpresswoocommerceordersemail-notifications

Go through certain WooCommerce orders, collect data and then send the results via email


The purpose of my code is to go through all orders from yesterday. Then I want the:

  • subtotal
  • tax
  • gratuity

count together and then send the results via the WordPress wp_mail() function.

For some reason it never even sends an email. Can someone tell me what is wrong with my code? do I miss something? any advice?

<?php
define('WP_USE_THEMES', false);


require( dirname( __FILE__ ) . '/wp-load.php' );


function dcwd_status_set_html_content_type() {
  return 'text/html';
}

$yesterday = date( 'Y-m-d', strtotime( '-1 days' ) );

$args = array(
    'date_created' => $yesterday,
);
$orders = wc_get_orders( $args );

$subtotal = 0.0;
$gratuity = 0.0;
$taxxes = 0.0;
if ( count( $orders ) ) {
    $orders_by_status = array();
    foreach ( $orders as $order ) {
        $eachordersubtotal = $order->get_subtotal();
        $eachordersubtotal + $subtotal;
        $eachordergratuity = $order->get_fees();
        $eachordergratuity + $gratuity;
        $eachordertaxxes = $order->get_taxxes();
        $eachordertaxxes + $taxxes;
}
$subtotalstring = sprintf("%.3f", $subtotal);
$gratuitystring = sprintf("%.3f", $gratuity);
$taxxesstring = sprintf("%.3f", $taxxes);
$to = '[email protected]';
$subject = 'Order totals for yesterday';
$body = $subtotalstring, $gratuitystring, $taxxesstring;

wp_mail($to, $subject, $body)
?>

Solution

  • Your code contains a lot of mistakes

    • $orders_by_status is defined, but not used
    • $order->get_fees() will return an array, and not an integer
    • $order->get_taxxes() should be $order->get_taxes(), however this will also return an array so use $order->get_total_tax() instead
    • $eachordersubtotal + $subtotal should be replaced by $eachordersubtotal += $subtotal
    • The $headers at wp_mail() are missing

    So you get:

    // Date
    $yesterday = date( 'Y-m-d', strtotime( ' -1 days ' ) );
    
    // Args
    $args = array(
        'date_created' => $yesterday,
    );
    
    // Get WC orders
    $orders = wc_get_orders( $args );
    
    // Initialize
    $subtotal = 0;
    $gratuity = 0;
    $taxes = 0;
    
    // NOT empty
    if ( ! empty ( $orders ) ) {
        foreach ( $orders as $order ) {
            // DEBUG information, removed if desired
            echo '<p>ID = ' . $order->get_id() . '</p>';
            
            // Get subtotal
            $subtotal += $order->get_subtotal();
            
            // Get fees
            foreach ( $order->get_fees() as $fee_id => $fee ) {
                $gratuity += $fee['line_total'];
            }
    
            // Get tax
            $taxes += $order->get_total_tax();
        }
    }
    
    // Send e-mail
    $to = '[email protected]';
    $subject = 'Order totals for yesterday';
    $body = '<p>Subtotal = ' . $subtotal . '</p><p>Gratuity = ' . $gratuity . '</p><p>Taxes = ' . $taxes . '</p>';
    $headers = array( 'Content-Type: text/html; charset=UTF-8' );
    
    wp_mail( $to, $subject, $body, $headers );