Reputation: 5493
I want to show the best selling products of the last 30 days. I already have a code for showing bestseller products. But these are the total sales of the shop. Is there any option to limit the product sales to 30 days?
I guess the meta key total_sales
isn't the correct way to do this, right?
Here's my current loop:
$args_basic = array(
'post_type' => 'product',
'posts_per_page' => 12,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
);
Upvotes: 1
Views: 1265
Reputation: 5449
I've no way to test the following but at first glance it should be working from the gecko.
wc_get_orders
and the date_after
argument.Array
.array_count_values()
to count the number of time each IDs shows up.<?php
add_action( 'init', 'wpso_67421248' );
function wpso_67421248() {
$orders = wc_get_orders(
array(
'limit' => -1,
'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ),
'date_after' => date( 'Y-m-d', strtotime( '-1 month' ) ),
'return' => 'ids',
)
);
$identifiers = array();
foreach ( $orders as $order ) {
$items = wc_get_order( $order )->get_items();
foreach ( $items as $item ) {
array_push( $identifiers, $item->get_product_id() );
};
};
var_dump( rsort( array_count_values( $identifiers ) ) );
};
Upvotes: 2