None of my woocommerce functions are working

  php, woocommerce, wordpress

So i have this 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. I think I might be missing a dependency or something. Btw this is a nightly running cron job.

<?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 = '[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 );
?>

Source: Ask PHP

LEAVE A COMMENT