1

Based on Go through certain WooCommerce orders, collect data and then send the results via email anwer code that is supposed to take all the WooComerce orders from yesterday total them and email them to me, but the email prints 0 for all the values.

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


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

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

// Args
$args = array(
    'date_created' => $yesterday,
);

// Get WC orders
$orders = Wc()->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 = 'jesse@munerismedia.com';
$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 );
?>

I think I might be missing a dependency or something. Btw this is a nightly running cron job.

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
GGNoEffort
  • 17
  • 3
  • I have no idea why you replaced `$orders = wc_get_orders( $args );` by `$orders = Wc()->wc_get_orders( $args );` from my answer. Rather than just making some tweaks and then finding that it doesn't work, you should use [debug and/or error reporting](https://stackoverflow.com/questions/61740111/how-to-debug-in-woocommerce-3), since the codes your post contain errors and will generally show an error message. Based on that information, you will know where things go wrong – 7uc1f3r Sep 26 '21 at 09:20
  • Ahh I see i uploaded the code from a test i was doing it wasnt working even with this line correct. $orders = wc_get_orders( $args ); – GGNoEffort Sep 26 '21 at 20:43

0 Answers0