WooCommerce [products] shortcode has many features but lacks some ‘template’ feature. How can I define custom template like this:
[products class="simple-list"]
WooCommerce [products] shortcode has many features but lacks some ‘template’ feature. How can I define custom template like this:
[products class="simple-list"]
Open your theme’s function.php and add following
add_action( 'woocommerce_shortcode_before_products_loop', 'roka_before_products_shortcode_loop', 1, 10 );
add_action( 'woocommerce_shortcode_after_products_loop', 'roka_after_products_shortcode_loop', 0, 10 );
function roka_before_products_shortcode_loop( $atts ) {
$GLOBALS[ 'roka_woocommerce_loop_template' ] =
( isset( $atts[ 'class' ] ) ? $atts[ 'class' ] : '' );
}
function roka_after_products_shortcode_loop( $atts ) {
$GLOBALS[ 'roka_woocommerce_loop_template' ] = '';
}
Then override the file content-product.php
by copying it to your theme’s woocommerce
folder.
find the line <li <?php post_class(); ?>>
and add following after that:
<?php
if(
isset( $GLOBALS[ 'roka_woocommerce_loop_template' ] ) &&
$GLOBALS[ 'roka_woocommerce_loop_template' ] == 'simple-list'
) {
echo '<a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
return;
}
?>