Skip to content

Instantly share code, notes, and snippets.

@hamidrezayazdani
Last active June 14, 2021 09:43
Show Gist options
  • Save hamidrezayazdani/c0e2f0be5142ee12691dca0b09337114 to your computer and use it in GitHub Desktop.
Save hamidrezayazdani/c0e2f0be5142ee12691dca0b09337114 to your computer and use it in GitHub Desktop.
<?php
/**
* add shortcode to wp
*/
add_shortcode( 'stock_status', 'display_product_stock_status' );
function display_product_stock_status( $atts) {
$atts = shortcode_atts(
array('id' => get_the_ID() ),
$atts,
'stock_status',
);
$product = wc_get_product( $atts['id'] );
if ( ! $product ) {
return null;
}
return wc_get_stock_html( $product );
}
// The code goes to your active theme (or child theme) functions.php
/**
* Usage:
* Place [stock_status id="YOUR PRODUCT ID"] everywhere you want to show stock status
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment