I'm trying to create a custom Woocommerce products shortcode that allows me to offset a certain number of products when adding the shortcode to a page. I've been able to get the offset part working by looking at WooCommerce - Recent Products Offset and https://ericwijaya.com/load-woocommerce-loop-custom-arguments/
The problem I am having is that the Woocommerce products "category" attribute doesn't work with my new shortcode. My shorcode looks like this: [products_offset limit="8" category="fiction" offset="10" orderby="menu_order"]
Instead of displaying products using the specified category slug (in this case "fiction"), it displays all products across all categories.
This is the code I've added to my function.php:
function products_offset_func( $atts ) {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'offset' => 0,
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), (array) $atts );
ob_start();
$query_args = array(
'columns' => $atts['columns'],
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'offset' => $atts['offset'],
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => WC()->query->get_meta_query(),
);
?>
<ul class="products">
<?php
$loop = new WP_Query( $query_args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
<?php
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'products_offset', 'products_offset_func' );`
Any help would be greatly appreciated!