首页 > 解决方案 > 我在这段代码中的 for 循环上限为 10 任何想法为什么会这样?

问题描述

所以我有这个代码,它从昨天开始接受 woocommerce 订单并在电子邮件中打印有关它们的信息,出于某种原因,它最多只能通过我的 for 循环 10 次,我不太明白为什么任何指导都会很棒。

<?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 );
?>

标签: phpwordpresswoocommerce

解决方案


文档(强调我的):

限制

接受整数:要检索的最大结果数或 -1 表示无限制。

默认值:站点“posts_per_page”设置。

所以您的posts_per_page设置可能是 10。要全部获取它们,您需要将 limit 选项添加到您的 args 数组中:

$args = array(
    'limit' => -1,
    'date_created' => $today,
);
$orders = wc_get_orders( $args );

推荐阅读