I've got the following custom shortcode to display a range of products
add_shortcode( 'my_shortcode_name', 'on_sale_products' );
function on_sale_products() {
global $product, $woocommerce, $woocommerce_loop;
$args = apply_filters('woocommerce_related_products_args', array(
// this is working array, just empty for this example
)
);
$products = new WP_Query( $args );
ob_start();
woocommerce_product_loop_start();
while ( $products->have_posts() ) : $products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="on-sale">' . ob_get_clean() . '</div>';
}
I am trying to add a text message inside the loop that will say "No products to display" if there no products to show.
I'm struggling to correctly place the statement without getting a syntax error.
I've been fiddling around with the code like this:
add_shortcode( 'my_shortcode_name', 'on_sale_products' );
function on_sale_products() {
global $product, $woocommerce, $woocommerce_loop;
$args = apply_filters('woocommerce_related_products_args', array(
// this is working array, just empty for this example
)
);
$products = new WP_Query( $args );
ob_start();
woocommerce_product_loop_start();
if ( $products->have_posts() ) : $products->the_post() {
wc_get_template_part( 'content', 'product' );
} else {
echo '<div class="no-products">There are no products to display</div>';
}
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="on-sale">' . ob_get_clean() . '</div>';
}
But that's not correct.
Could you please point me to the right direction?