1

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

What would be the PHP snippet to display the remaining numbers of product currently available (basically In Stock) ?

ex: Hurry Up! Only 10 trades (woocommerce -> products) available!

Thanks in advance!

I tried the code provided :

function fp2() {
    global $wpdb; 

    $stock = get_post_meta( $post->ID, '_stock', true ); 

    echo '<span style="color:#fff;text-align:center;font-size:12px">Remaining Trade:' . $stock;
}
add_shortcode('fp7', 'fp2');
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
enkiki
  • 389
  • 1
  • 3
  • 10
  • Possible duplicate of [Woocommerce - Showing only products in stock](https://stackoverflow.com/questions/23412782/woocommerce-showing-only-products-in-stock) – Dhaval Shah Dec 14 '17 at 05:54
  • I would suggest to look at accepted answer for linked duplicate question above as it 'caches' the result otherwise you would run an expensive SQL query on every page load where this function is called. – Dhaval Shah Dec 14 '17 at 05:56

1 Answers1

4

Updated (2021)

Here is a custom function with a SQL query that will return the products "instock" count:

function get_instock_products_count(){
    global $wpdb;

    // The SQL query
    $result = $wpdb->get_var( "
        SELECT COUNT(p.ID)
        FROM {$wpdb->prefix}posts as p
        INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
        WHERE p.post_type LIKE '%product%'
        AND p.post_status = 'publish'
        AND pm.meta_key = '_stock_status'
        AND pm.meta_value = 'instock'
    " );
    
    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_instock_products_count();
$message = sprintf( __( 'Hurry Up! Only %s remaining trades' ), $count );
echo '<div class="woocommerce-message">'.$message.'</div>';

will display something like:

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Awesome! Thank you so much! Not sure If I can ask another question here (new to this platform) but here it is in case or let me know if I should post a fresh question on this site.... Same basis.. is there a way to display the number of products that has been published today (new) – enkiki Dec 13 '17 at 11:52
  • Hi again, sorry about this, im pretty new on this site, i clicked on your solution now. Agreed with your solution more. Thank you, apologize – enkiki Dec 13 '17 at 15:05
  • @enkiki I have maid an answer then… you can check it if ou like. – LoicTheAztec Dec 13 '17 at 16:06
  • Thank you very much Loic, awesome! I created another question which combine both of your solution in one but no luck,,, https://stackoverflow.com/questions/47809990/woocommerce-combine-3-php-functions-in-one Feel free to take a look :) – enkiki Dec 14 '17 at 09:32
  • Hi LoicTheAztec, if you have time to read my new question that would be awesome and very very appreciated... I'm basically just waiting for the right solution and move forward! Even willing to pay you tip !! :) – enkiki Dec 14 '17 at 14:27
  • Salut Loic, c'etait plutot la question " https://stackoverflow.com/questions/47809990/woocommerce-combine-3-php-functions-in-one " .. combine vos deux scripts ensemble. Merci de votre cousine Quebecoise :) – enkiki Dec 15 '17 at 04:03
  • HP Fatal error: Uncaught TypeError: reset() – skywind Jul 18 '21 at 23:56