0

I have a WooCommerce site where products are considered as trade/deal. Therefore, when someone take a trade (buy a product), it become out of stock.

How to display the number of products that has been published today (the new ones)?

ex: 4 New Trades (products) Published Today

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
enkiki
  • 389
  • 1
  • 3
  • 10
  • Please go read [ask]. SO is not a place to provide you with free code snippets “on order.” Show us what you have done so far to try and solve this. – CBroe Dec 13 '17 at 15:10
  • You did the same thing with your previous question already, https://stackoverflow.com/q/47791495/1427878 And since that was about a very similar issue, the least I’d expect from you now is that you show us that you _learned something_ from the answer you got on that other question, and were at least able to come up with some approach to this yourself. – CBroe Dec 13 '17 at 15:12

1 Answers1

2

The custom function below will return with a SQL query the newly published products count (made in the 24 hours):

function get_new_products_count(){
    global $wpdb;
    
    // 24 hours ago
    $is_24h_ago = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s")." -1day"));

    // The SQL query
    $result = $wpdb->get_col( "
        SELECT COUNT(p.ID)
        FROM {$wpdb->prefix}posts as p
        WHERE p.post_type LIKE '%product%'
        AND p.post_status LIKE 'publish'
        AND p.post_date > '$is_24h_ago'
    " );

    return reset($result);
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and working


Usage example (in any php file):

$count = get_new_products_count();
$message = sprintf( __( '%s new trades have been published…' ), $count );
echo '<div class="woocommerce-message">'.$message.'</div>';

will display something like:

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399