Вот мой код. далеко:
Код: Выделить всё
function display_total_amount_spent( $user_id=null ) {
if ( empty($user_id) ){
$user_id = get_current_user_id();
}
$today_year = date( 'Y' );
$today_month = date( 'm' );
$day = date( 'd' );
if ($today_month == '01') {
$month = '12';
$year = $today_year - 1;
} else{
$month = $today_month - 1;
$month = sprintf("%02d", $month);
$year = $today_year - 1;
}
$end_date = 'user_selected_date_period';
$single = true;
$fetch_end_date = get_user_meta($user_id, $end_date, $single);
$convert_ed_to_time = strtotime($fetch_end_date);
$now = strtotime('now');
$gap_days = $convert_ed_to_time;
$gap_days_in_seconds = 60*60*24*$gap_days;
$gap_time = $now - $gap_days_in_seconds;
$args = array(
'post_type' => 'shop_order',
'post_status' => array( 'wc-completed', 'wc-processing' ),
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'date_query' => array(
'relation' => 'OR',
array(
'year' => $today_year,
'month' => $today_month,
),
array(
'year' => $year,
'month' => $month,
),
),
);
$customer_orders = get_posts( $args );
$count = 0;
$total = 0;
$no_orders_message = __('No orders found the date period', 'hello-world');
if (!empty($customer_orders)) {
$customer_orders_date = array();
foreach ( $customer_orders as $customer_order ){
$customer_order_date = strtotime($customer_order->post_date);
if ( $customer_order_date > $gap_time ) {
$customer_order_date;
$order = new WC_Order( $customer_order->ID );
$order_items = $order->get_items();
$total += $order->get_total();
foreach ( $order_items as $order_item ){
$count++;
}
}
}
return floatval( preg_replace( '#[^\d.]#', '', $total, $count ) );
} else {
return $no_orders_message;
}
}
add_shortcode( 'user_amount_spent', 'display_total_amount_spent' );
Любые указатели или рекомендации будут такими очень ценю!
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/790 ... date-range