-1

So I have this code that takes woocommerce orders from yesterday and prints information about them in an email for some reason it is only going through my for loop a max of 10 times and i dont quite understand why any guidance would be fantastic.

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


require( dirname( __FILE__ ) . '/wp-load.php' );
// Date
date_default_timezone_set('PST');
$today = date( 'Y-m-d' );

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

// Get WC orders
$orders = \wc_get_orders( $args );

// Initialize
$subtotal = 0;
$gratuity = 0;
$taxes = 0;

// NOT empty
if ( ! empty ( $orders ) ) {
    foreach ( $orders as $order ) {

        echo $order->get_id();
        // 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();
    }
}
$convenience = $gratuity;
$gratuity -= .04 * $subtotal;

echo 'Date = ' . $today . ' Subtotal = ' . $subtotal . ' Convenience Fee' . $convenience . ' Gratuity = ' . $gratuity . ' Taxes = ' . $taxes . '';
// Send e-mail
$to = 'jesse@munerismedia.com';
$subject = 'Order totals for today';
$body = '<p>Date = ' . $today . '</p><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 );
?>
GGNoEffort
  • 17
  • 3
  • `'limit'` (earlier: `'numberposts'`) defaults to `get_option( 'posts_per_page' )` (10 in your case), see `wc_get_orders()`. **WC_Order_Query** and **WC_Object_Query** (bold ones are classes). Set it to `-1` for _all_ orders. – hakre Oct 01 '21 at 22:09

1 Answers1

4

From the documentation (emphasis mine):

limit

Accepts an integer: Maximum number of results to retrieve or -1 for unlimited.

Default: Site 'posts_per_page' setting.

So your posts_per_page setting is probably 10. To get them all, you need to add the limit option to your args array:

$args = array(
    'limit' => -1,
    'date_created' => $today,
);
$orders = wc_get_orders( $args );
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116