karlosuccess
karlosuccess

Reputation: 885

Count number of orders *ordered* after an specific order ID in WooCommerce

We are trying to implement a prize for the 10th user ordering from the website.

But it is not all time user orders. The contest start today so we need to count the number of orders after the last order last night.

I have the order id of the order last night.

How can I achieve this?

So far I have this:

$args = array();

$args['numberposts'] = 10;

$args['status'] = array( 'wc-completed', );

$args['ADDITIONAL_PARAMETER'] = ADDITIONAL_PARAMETER; /* orders after and specific order */

$orders = wc_get_orders( $args );

Can you help?

Upvotes: 1

Views: 1790

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

You can use wc_get_orders and WC_Order_Query that provide a standard way of retrieving orders that is safe to use and will not break due to database changes in future WooCommerce versions.

Source: wc_get_orders and WC_Order_Query - Just pass a series of arguments that define the criteria for the search.

So you get:

// Order ID last night
$order_id_last_night = 2448;

// Get $order object
$order_last_night = wc_get_order( $order_id_last_night );

// Get date created
$order_last_night_date_created = $order_last_night->get_date_created();

// Get 10 order IDs after date last night
$orders_ids = wc_get_orders( array(
    'status'       => array( 'wc-completed' ),
    'limit'        => 10,
    'orderby'      => 'date',
    'order'        => 'ASC',
    'date_created' => '>' . strtotime( $order_last_night_date_created ),
    'return'       => 'ids',
));
 
// Consists of 10
if( count( $orders_ids) == 10 ) {
    // Returns the value of the last element
    echo end( $orders_ids );
}

Upvotes: 2

Related Questions