Displaying Product Stock Levels

Hi guys,
I can't seem to find a way to display the remaining stock/inventory level for each product on the frontend.
I'm using the "mp_product_stock" shortcode which works fine on the actual product page if I insert it in the description, but it shows "Unlimited Stock" when it's on the store/product listings page when used in the excerpt. Does anybody have any suggestions? It would be great if there was an option in the product page itself to simply choose whether or not to display the stock levels.
Thanks in advance.

  • Predrag Dubajic

    Hi Jamie,

    Hope you're doing well today :slight_smile:

    I have tested this on my installation by adding [mp_product_stock product_id="10"] shortcode inside product excerpt and when I check my posts page the number of items left in stock is displayed properly for me:

    Can you tell me if I'm missing something here or could you provide some additional steps that we can follow in order to replicate this?

    Also, would you mind allowing support access so we can have a closer look at your setup?
    To enable support access you can follow this guide here:
    https://premium.wpmudev.org/docs/getting-started/getting-support/#chapter-5

    Best regards,
    Predrag

  • Jamie

    Hi Predrag,
    Thanks for your reply!

    I didn't include the product_id part of the shortcode, I was presuming it wouldn't need it because of it already being in the product loop (I'm obviously wrong).

    I've changed my code to include the id so it now works as you suggested, but it'll mean having to add a different code for every single product! I can manage it myself for my own small websites, but when I create an ecommerce website for a customer, I can't expect them to do that for every product, especially if it's a large website. Do you think there's a way for MarketPress to add it as an option? Or will I need to look at coding it into a template?

  • Adam Czajczyk

    Hi Jamie,

    With a lot of products that might indeed be a daunting task to add all these IDs. However, I think we could simplify that a little with this custom shortcode:

    <?php 
    
    //automatically feed [mp_product_stock product_id=" "] shortcode
    //with product ID
    
    function auto_mp_product_stock_shortcode() {
    	global $post;
    	return do_shortcode('[mp_product_stock product_id="' . $post->ID .'"]');
    }
    add_shortcode('mp_product_stock_auto', 'auto_mp_product_stock_shortcode');

    How to use it:

    1. Add that code as an MU plugin, as follows:

    - create an empty file with .php extension (e.g. "auto-mp-product-stock-shortcode.php")
    - paste the code from above into that file, using a "clean text" editor (such as e.g. Notepad++, Sublime or similar)
    - upload the file into the "/wp-content/mu-plugins" folder of your WP install; if there's no "mu-plugins" folder inside "wp-content" folder, just create it

    2. use this shortcode in your product descriptions instead of the native one:

    [mp_product_stock_auto]

    I checked that on my test setup and it seems to be working fine there. Let me know if it works for you too, please.

    Best regards,
    Adam

  • Jamie

    Hi Adam,

    Yes it's in the excerpt field. I'm using XTheme which is great apart from when needing to create custom templates. It calls in other templates from folders and it all gets very confusing to work out exactly what's being edited.

    I've spent all weekend trying to customise the product page and the product listings but I've not managed to do a lot with them at all. I think I'm going to try Woocommerce and see how that works, and if that still doesn't work out too well then I think I'll have to try out another theme. I might give an Upfront theme another chance, I struggled with Fixer last time but I'm sure it'll integrate with MarketPress better than XTheme does.

  • Predrag Dubajic

    Hi Jamie,

    I managed to get a hold of X theme, few versions back but it's near the latest one, and I can see the issue there as well.

    So when I'm using [mp_product_stock], even without ID, it works fine with default WP theme but not with the X theme.

    I was unable to find any similar reports of shortcodes within X theme excerpt so it might be best getting in touch with theme developers and they should be able to give you some more suggestions on this.

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.