现在已经很晚了,因为我正在键入这段代码,所以请对下面的代码持保留态度。
我认为如果您修改这里提供的代码(下面的修改版本供将来参考),https://businessbloomer.com/woocommerce-custom-cron-job/ (我不是该网站的附属机构),你应该把事情做好。
你需要改变的是part D. 添加foreach循环以遍历所有匹配的订单。使用订单id获取所需订单和自定义元数据,创建并发送电子邮件。你可以使用一个助手函数来创建电子邮件正文,也可以在循环中完成这项工作——我认为这是一个品味问题。
您可能还想更改计划。在原始代码示例中,它被设置为三个小时。对你来说,也许每天一次更合适,是吗?
<?php
/**
* @snippet Schedule Email to WooCommerce Admin Every 3 Hours
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=106360
* @author Rodolfo Melogli
* @compatible WooCommerce 3.5.4
*/
// ---- ---- ----
// A. Define a cron job interval if it doesn\'t exist
add_filter( \'cron_schedules\', \'bbloomer_check_every_3_hours\' );
function bbloomer_check_every_3_hours( $schedules ) {
$schedules[\'every_three_hours\'] = array(
\'interval\' => 10800, // <= you might want to change this to something less frequent, once daily?
\'display\' => __( \'Every 3 hours\' ),
);
return $schedules;
}
// ---- ---- ----
// B. Schedule an event unless already scheduled
add_action( \'wp\', \'bbloomer_custom_cron_job\' );
function bbloomer_custom_cron_job() {
if ( ! wp_next_scheduled( \'bbloomer_woocommerce_send_email_digest\' ) ) {
wp_schedule_event( time(), \'every_three_hours\', \'bbloomer_woocommerce_send_email_digest\' );
}
}
// ---- ---- ----
// C. Trigger email when hook runs
add_action( \'bbloomer_woocommerce_send_email_digest\', \'bbloomer_generate_email_digest\' );
// ---- ---- ----
// D. Generate email content and send email if there are completed orders
function bbloomer_generate_email_digest() {
$range = 180; // 3 hours in minutes
$completed_orders = bbloomer_get_completed_orders_before_after( strtotime( \'-\' . absint( $end ) . \' MINUTES\', current_time( \'timestamp\' ) ), current_time( \'timestamp\' ) ); // Change parameters to compare desired time period
if ( $completed_orders ) {
// Untested, but something like this should work
foreach ( $completed_orders as $completed_order_id ) {
$customer_email = get_post_meta( $completed_order_id, \'customer_email\', true );
$order = wc_get_order( $completed_order_id );
if ( $customer_email ) {
$email_subject = "Gig feedback";
$email_content = get_email_content( $order );
wp_mail( $customer_email, $email_subject, $email_content );
}
}
}
}
// Helper function to make the email loop a little cleaner
function get_email_content( $order ) {
// Return email content
}
// ---- ---- ----
// E. Query WooCommerce database for completed orders between two timestamps
function bbloomer_get_completed_orders_before_after( $date_one, $date_two ) {
global $wpdb;
$completed_orders = $wpdb->get_col(
$wpdb->prepare(
"SELECT posts.ID
FROM {$wpdb->prefix}posts AS posts
WHERE posts.post_type = \'shop_order\'
AND posts.post_status = \'wc-completed\'
AND posts.post_modified >= \'%s\'
AND posts.post_modified <= \'%s\'",
date( \'Y/m/d H:i:s\', absint( $date_one ) ),
date( \'Y/m/d H:i:s\', absint( $date_two ) )
)
);
return $completed_orders;
}
另外,我明天会尽量记住再看一遍。