Created
May 7, 2021 09:33
-
-
Save AchalJ/7a429ef46a5c8f8c61cda81bbefcf374 to your computer and use it in GitHub Desktop.
WooCommerce Display Stock Status with custom text through Shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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