0

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)?

Community
  • 1
  • 1
Liv
  • 331
  • 3
  • 7

1 Answers1

1

Got the answer from Lorro:

One should create 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
}
Liv
  • 331
  • 3
  • 7