How to add a custom message
-
I would like to be able to add a check box to the inventory section of Add/Edit Products that when checked will display a message on the single product summary section. I only need to display this message on some products when they are marked Out of Stock, so I thought the best way is to have an option to display the message or not.
I found several bits and pieces on how to do something similar but can’t get it to work properly. This is what I have so far:
The following code adds the check box where I want:
add_action( ‘woocommerce_product_options_inventory_product_data’, ‘wc_rose_product_field’ );
function wc_rose_product_field() {
woocommerce_wp_checkbox( array( ‘id’ => ‘rose_message’, ‘wrapper_class’ => ‘show_if_simple show_if_variable’, ‘label’ => __(‘Rose Sold Out message’, ‘woocommerce’), ‘description’ => __(‘Enable this to display rose sold out message’, ‘woocommerce’) ) );
}This bit of code will save it to the postmeta table in the database if checked :
add_action( ‘save_post’, ‘wc_rose_save_product’ );
function wc_rose_save_product( $product_id ) {
if ( ! empty( $_POST[‘rose_message’] ) ) {
update_post_meta( $product_id, ‘rose_message’, ‘yes’ );
} else {
update_post_meta( $product_id, ‘rose_message’, ‘ ‘ );
}
}Now I think I want an if statement but I have tried lots of things and I cant get it to work.
I thought I could use something like this:
add_action(‘woocommerce_single_product_summary’, ‘wc_rose_show’, 10);
function wc_rose_show() {
if (get_post_meta( ‘rose_message’ ) == ‘yes’ ) {
echo ‘Test message’;
}
}
If anyone has any ideas how I can achieve this I will be very grateful.Thanks
- The topic ‘How to add a custom message’ is closed to new replies.