-
How can one create a Generate Blocks query loop block for Woocommerce products that excludes out-of-stock items?
Thank you
-
Hi there,
You will need to alter the query arguments by writing custom PHP code.
1. Add this PHP code:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // apply filter if loop has class: in-stock-products if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'in-stock-products' ) !== false ) { $query_args = array_merge( $query_args, array( 'meta_query' => array( array( 'key' => '_stock_status', 'value' => 'instock', 'compare' => '=', ), ), )); } return $query_args; }, 10, 2 );
Adding PHP: https://docs.generatepress.com/article/adding-php/
2. Add
in-stock-products
class to the Grid block of the query loop.
Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/Let me know if this helps!
-
Worked perfectly.
Thank you
-
You are welcome 🙂
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.