Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Customize Product Filters in Woocommerce Site WordPress

Site is http://horsecreek.flywheelsites.com

We have Woocommerce setup but we want the appearance of two stores: one for wine, and one for a "gift shop."

The wine page and filter are working perfectly: http://horsecreek.flywheelsites.com/?post_type=product

But the gift shop is not displaying the products the way we need them to.
http://horsecreek.flywheelsites.com/gift-shop/

The search and price filter work great, but the category filter only shows the wine categories initially. This is because I had to add code to the functions.php file to only have the wine categories show on the wine page.

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {

$new_terms = array();

// if a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {

foreach ( $terms as $key => $term ) {

if ( ! in_array( $term->slug, array( 'nuts', 'gifts', 'flour', 'oils', 'pickles', 'honey', 'jellies-syrup', 'salsa', 'sauce', 'juice' ) ) ) {
$new_terms[] = $term;
}

}

$terms = $new_terms;
}

return $terms;
}

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;

if ( ! is_admin() && is_shop() ) {

$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'gifts','nuts','flour','oils','pickles','honey','jellies-syrup','salsa','sauce','juice' ), // Don't display products in these categories on the shop page
'operator' => 'NOT IN'
)));

}

remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}


On this page, we want the product category filter to show all the categories NOT related to wine (gifts, nuts, flour, oils, pickles, honey, jellies-syrup, salsa, sauce, juice).

Answers (2)

2016-03-22

Andrea P answers:

Hello Anne,

I am sorry but I think your problem is that your solution for the wines page was quite aggressive and unnecessary..

instead of building up a filter which apply to all queries and all get_terms, you should have made a simple custom page-template for wines
adding a custom query in which you retrieve only the consistent categories.

or another thing that you could have done is to create a parent category called "wine" and place all the relevant categories as sub-categories of wine.

at that point, if you visit the archive for the product category "wine", you'd see only the products within the wine's categories

and you can do the same with the gifts, placing them all under a subcategory..


also, did you know that your shop main page url is this one?
http://horsecreek.flywheelsites.com/shop/

why are you using this in the top menu
http://horsecreek.flywheelsites.com/?post_type=product

this last url will display all products from the post type product, while the other one is the actual woocommerce shop main page.


let me know your thoughts!


Anne Shenton comments:

Hi Andrea, I'm so sorry for my delayed response. Thanks for your suggestion and I agree we are probably making this much harder than it needs to be. I tried your suggestion of adding a parent category for wine and while that displays the wines on the page, the top filter shows ALL categories and I only want it to show the wine-related categories. Same goes on the gift shop pages (to only show the gift categories in the filter).

Here's what the wine page looks like now (with the filter): http://horsecreek.flywheelsites.com/product-category/wine/

2016-03-23

Reigel Gallarde answers:

Same as what Andrea P is saying.. aggressive and unnecessary..


here's what I can suggest though... create pages and use product_category shortcode...

example

create Wine page and use
[product_category operator"NOT IN" category="gifts, nuts, flour, oils, pickles, honey, jellies-syrup, salsa, sauce, juice"]

create Gift shop page and use
[product_category category="gifts, nuts, flour, oils, pickles, honey, jellies-syrup, salsa, sauce, juice"]

you will then add this pages to the menu...

above are just suggestions to make things better (my opinion)..

about your product category filter, I believe you are using woof, you need to add your excluded term ids, attached is a sample screen shot..