Below, you’ll find a detailed guide on how to add the Out of Stock Message for WooCommerce Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Out of Stock Message for WooCommerce Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Out of Stock Message for WooCommerce Plugin and the shortcodes it provides:
"Out of Stock Message for WooCommerce is a handy plugin that allows WooCommerce store owners to showcase customized out of stock messages to their customers, enhancing user experience."
- [wcosm_stockout_msg]
Out of Stock Message for WooCommerce [wcosm_stockout_msg] Shortcode
The ‘wcosm_stockout_msg’ shortcode is a part of the WC-Out-of-Stock-Message plugin. It displays a custom out-of-stock message for WooCommerce products. This shortcode checks the product’s stock status. If the product is out of stock, it retrieves the custom message saved in the product’s meta-data, or a global message set in the plugin’s options. The message is then displayed within a styled div element.
Shortcode: [wcosm_stockout_msg]
Examples and Usage
Basic example – In this example, we are using the shortcode without any attributes. This will display the out of stock message as set in the product’s metadata or the global out of stock message if it is enabled.
[wcosm_stockout_msg /]
Advanced examples
The ‘wcosm_stockout_msg’ shortcode doesn’t directly accept any attributes. However, you can manipulate its output by changing the ‘_out_of_stock_msg’ and ‘_wcosm_use_global_note’ metadata of the product. For instance, you can use the ‘update_post_meta’ function to change these values before the shortcode is executed.
// Update the out of stock message for a product with ID 123
update_post_meta(123, '_out_of_stock_msg', 'Sorry, this item is currently unavailable.');
// Enable the global out of stock message for the same product
update_post_meta(123, '_wcosm_use_global_note', 'yes');
// Now use the shortcode
echo do_shortcode('[wcosm_stockout_msg /]');
Keep in mind that the changes made with ‘update_post_meta’ will persist in the database. Therefore, this method is best used in a context where you want to permanently change the out of stock message for a specific product.
PHP Function Code
In case you have difficulties debugging what causing issues with [wcosm_stockout_msg]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wcosm_stockout_msg', array($this,'wcosm_get_shortcode') );
Shortcode PHP function:
function wcosm_get_shortcode ( $atts, $key = "" )
{
/*get output*/
global $post, $product;
$get_saved_val = get_post_meta( $post->ID, '_out_of_stock_msg', true);
$global_checkbox = get_post_meta($post->ID, '_wcosm_use_global_note', true);
$global_note = get_option('woocommerce_out_of_stock_message');
if( $get_saved_val && !$product->is_in_stock() && $global_checkbox != 'yes') {
return sprintf( '<div class="outofstock-message">%s</div> <!-- /.outofstock-product_message -->', $get_saved_val );
}
if( $global_checkbox == 'yes' && !$product->is_in_stock() ) {
return sprintf( '<div class="outofstock-message">%s</div> <!-- /.outofstock_global-message -->', $global_note );
}
return false;
}
Code file location:
wc-out-of-stock-message/wc-out-of-stock-message/out-of-stock-msg.php
Conclusion
Now that you’ve learned how to embed the Out of Stock Message for WooCommerce Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply