我有这个代码。当我将此代码放入网站时,电子邮件功能开始每秒运行多次。我已经安排了每天。另外,如果可能的话,我希望它在特定时间显示。
if( !wp_next_scheduled( 'slack_refresh' ) ) {
wp_schedule_event( time(), 'daily', 'slack_refresh' );
}
add_action( 'slack_refresh', 'update_sales' );
function update_sales() {
// put your code here
$orders_count = get_daily_orders_count('2018-07-04');
$final_total_fahad="Today you received ".$orders_count." orders and a total sale of ".strip_tags(wc_price(get_daily_purchases_total()));
wp_mail("abc@gmail.com", "Order Count",$final_total_fahad );
}
function get_daily_orders_count( $date = 'now' ){
if( $date == 'now' ){
$date = date("Y-m-d");
$date_string = "> '$date'";
} else {
$date = date("Y-m-d", strtotime( $date ));
$date2 = date("Y-m-d", strtotime( $date ) + 86400 );
$date_string = "BETWEEN '$date' AND '$date2'";
}
global $wpdb;
$result = $wpdb->get_var( "
SELECT DISTINCT count(p.ID) FROM {$wpdb->prefix}posts as p
WHERE p.post_type = 'shop_order' AND p.post_date $date_string
AND p.post_status IN ('wc-on-hold','wc-processing','wc-completed')
" );
return $result;
}
function get_daily_purchases_total(){
global $wpdb;
return $wpdb->get_var( "
SELECT DISTINCT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-processing','wc-completed')
AND DATE(p.post_date) >= DATE(NOW())
AND pm.meta_key LIKE '_order_total'
" );
}