首页 > 解决方案 > 我的 WooCommerce 功能都不起作用

问题描述

根据浏览某些 WooCommerce 订单,收集数据,然后通过电子邮件发送结果代码,该代码应该从昨天的所有 WooComerce 订单中汇总它们并通过电子邮件发送给我,但电子邮件打印的所有值均为 0。

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

我想我可能缺少依赖项或其他东西。顺便说一句,这是一个夜间运行的 cron 作业。

标签: phpwordpresswoocommerceorders

解决方案


推荐阅读