Shortcode for stock status as text
-
Hi,
I have found several references on how to create shortcode what would display a product stock amount by ID anywhere on the site. However I need it to show the same messages WooCommerce originally shows under product pages with the same shortcode.
For example I need to insert a product stock status on a page. I use a shortcode and it says something like “Stock is 3 pcs” or “Stock is 0 pcs”. What I need the shortcode to display is “In Stock” or “Out of Stock”.
Maybe I am not enough familiar with this but I haven’t been able to find anything and I don’t know how to modify the code I have myself. Can anyone help me, please?
Code I use right now:
// [show_qty id="product_id"] function show_qty_func( $atts ) { $p = shortcode_atts( array( 'id' => 0 ), $atts ); $_pf = new WC_Product_Factory(); $_product = $_pf->get_product($p['id']); return $_product->get_stock_quantity(); } add_shortcode( 'show_qty', 'show_qty_func' );
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Shortcode for stock status as text’ is closed to new replies.