Filtered product table
-
Hello,
I want to change the layout of the category page. I want to make one table where the products are displayed. This works, but the only challenge that I have is that all products are displayed (and thus ignoring the product filters). I tried to add tax_query arguments, but this didn’t seem to work.
Is there an easy way to only show the products in the table that match the filter requirements? An idea that I had was to retrieve the SKUs from the products that show on the filter and give them as an argument to the WP_Query, but I did not manage to complete this.
This is the code that I made so far.
add_action('woocommerce_archive_description','displaying_product_table'); function displaying_product_table() { $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 'tax_query' => array ( 'relation' => 'AND', array( 'taxonomy' => 'product_cat', 'field' => 'slug', // Or 'term_id' or 'name' 'terms' => get_query_var( 'product_cat' ) ), array( 'taxonomy' => 'pa_continent', 'field' => 'slug', // Or 'term_id' or 'name' 'terms' => get_query_var( 'pa_continent' ) ), array( 'taxonomy' => 'pa_degree', 'field' => 'slug', // Or 'term_id' or 'name' 'terms' => get_query_var( 'pa_degree' ) ) ) ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { echo "<div style='overflow-x:auto;'>"; echo "<table id='product-table'>"; echo "<tr> <th class='uni-image' width='10%'> <b> Image</b> </th> <th width='20%'> <b> Name </b> </th> <th width='10%'> <b> Category </b> </th> <th width='10%' onclick='sortTable()'> <b> Attribute1 </b> </th> <th width='10%'> <b> Attribute2 </b> </th> </tr>"; while ( $loop->have_posts() ) : $loop->the_post(); global $product; $product_id = $product->get_sku(); $product_image = $product->get_image(); $product_category = $product->get_categories(); $product_name = $product->get_name(); $uni_link = get_permalink( $product->get_id() ); $attribute1 = $product->get_attribute('pa_attribute1'); $attribute2 = $product->get_attribute('pa_attribute2'); echo "<tr> <td> $product_image </td> <td> <a href='$uni_link'> $product_name </a> </td> <td> $product_category </td> <td> $attribute1 </td> <td> $attribute2 </td> </tr>"; endwhile; wp_reset_query(); echo "</table>"; echo "</div>"; } else { echo __( 'No products found' ); } }
Help is appreciated
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Filtered product table’ is closed to new replies.