Skip to content

Instantly share code, notes, and snippets.

@AchalJ
Created May 7, 2021 09:33
Show Gist options
  • Select an option

  • Save AchalJ/7a429ef46a5c8f8c61cda81bbefcf374 to your computer and use it in GitHub Desktop.

Select an option

Save AchalJ/7a429ef46a5c8f8c61cda81bbefcf374 to your computer and use it in GitHub Desktop.
WooCommerce Display Stock Status with custom text through Shortcode
add_shortcode( 'woo_product_stock_status', function( $atts ) {
$atts = shortcode_atts(
array(
'id' => get_the_ID(),
'show_instock' => 'yes',
'show_outofstock' => 'yes',
'instock_text' => __( 'In Stock' ),
'outofstock_text' => __( 'Out of Stock' ),
),
$atts, 'woo_product_stock_status'
);
if ( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$product = wc_get_product( $atts['id'] );
$instock = $product->is_in_stock();
if ( $instock && 'yes' === $atts['show_instock'] ) {
return $atts['instock_text'];
}
if ( ! $instock && 'yes' === $atts['show_outofstock'] ) {
return $atts['outofstock_text'];
}
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment