I have created a function to display some products with a shortcode, but the problem I am having is that the errors messages are not shown on that page. For example if some fields are required then it's only displayed on the cart/checkout page.
Here's some of my code:
while ( $query->have_posts() ) : $query->the_post();
global $product;
?>
<div style="border-bottom:thin dashed black;margin-bottom:15px;">
<h2><?php the_title(); ?> <span><?php echo $product->get_price_html();?></span></h2>
<p><?php the_excerpt();?></p>
<?php global $product;
if( $product->is_type( 'simple' ) ){
woocommerce_simple_add_to_cart();
}
What do I need to add to show the error messages on the page the shortcode is being used on?
To make the WooCommerce notice actives in the page of your short code you need to add inside your shortcode this
wc_print_notices()
function.I have reproduced a similar Shortcode as yours below (for testing purpose) where woocommerce notices are printed:
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This is tested and works on WooCommerce 3+