• Resolved mif

    (@mifundum)


    I’d like to call the stock quantity of a simple product somewhere in a paragraph. For example i’m writing a blog and I’d like to name the available quantity like this:

    <p>Only <span class="quantity_product_1">
    <?php echo $product->get_stock_quantity(); ?></span> available!</p>

    I’ve come across something like this to create a function:

    <?php
        /**
        * Loop Price
        *
        * @author      WooThemes
        * @package     WooCommerce/Templates
        * @version     1.6.4
        */
    
        if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
        global $product;
        ?>
    
        <?php if ( $price_html = $product->get_price_html() ) : ?>
        <span class="price">PREIS:<span class="amount"><?php echo $price_html; ?></span></span><p class="stock-m13"><?php get_sku(get_the_ID()); ?></p>
        <?php endif; ?>

    And this:

    <p class=”stock-m13″><?php echo $product->get_stock_quantity(); ?></p>

    Would anyone know to get this done, how i can do this for a specific product (SKU)?

    https://www.ads-software.com/plugins/woocommerce/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Your code is for a loop, which this case isn’t. There’s probably different ways of doing this, mine would be to make a shortcode:

    // usage example: Only [product_stock id="1592"] available!
    add_shortcode('product_stock', 'product_stock');
    function product_stock( $atts ) {
      if ( ! $atts['id'] ) {
        return '';
      }
      $product = get_product($atts['id']);
      return $product->stock; // prints 22, ie the qty of product 1592
    }

    The code goes inf functions.php for your child theme.

    By SKU is a bit more complex.

    Thread Starter mif

    (@mifundum)

    Lorro, you’re my man! What a great quick and easy solution ??

    One more quick q, somehow some products are printed with and some without decimals.
    I’m looking for figures without (so not 6.0000 but 6). Is there an easy way to do that?

    Thanks!! Weehoee

    Try:

    return (int) $product->stock;

    Thread Starter mif

    (@mifundum)

    Thanks! ??

    Thread Starter mif

    (@mifundum)

    Marking as resolved

    Hi Lorro

    Thx for the snippet. However, what code to place in the the Worpdress page or post to display the quantity…excuse my ignorance.

    Best regards.

    Mazzedd

    Usage example: Only [product_stock id=”1592″] available!

    The product id comes from the list of products page.

    Thx Lorro, it makes it perfectly!
    Best regards

    Mazzedd

    Hi Lorro,

    Thanks for your shortcode!

    I tried to change your code, but it didn’t work out.
    (I’ve tried also with this one: https://www.ads-software.com/support/topic/shortcode-for-stock-status-as-text/-

    I want to make a page with an overview of 10 products and their availability.

    On the productpage I’ve changed the availability like this:

    
    add_filter( 'woocommerce_get_availability', 'mw_get_availability', 1, 2 );
    function mw_get_availability( $availability, $_product ) {
        global $product;
        // change text "In Stock' to 'SPECIAL ORDER' when quantity more than 6
        if ( $_product->is_in_stock() && $product->get_stock_quantity() > 15 ) $availability['availability'] = __('Meer dan 15 plaatsen beschikbaar', 'woocommerce');
        // change text to n Spots Left, where n is the quantity
        if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 15 ) $availability['availability'] = $product->get_stock_quantity() . __(' plaatsen beschikbaar');
        // change text "Out of Stock' to 'SOLD OUT'
        if ( !$_product->is_in_stock() ) $availability['availability'] = __('Deze trip is reeds volzet. Wil je op de wachtlijst komen? Stuur dan een mailtje naar [email protected] .', 'woocommerce');
    
        return $availability;
    }

    This I also would like to post newt to the 10 product on the availability overview page.

    Could you help me?

    Thx in advance!

    Sander

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to display stock quantity of a product in a post?’ is closed to new replies.