Skip Navigation

[Resolved] Display view results only for simple product type

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to display simple products with views or filter view to display simple products

Solution:
"product_type" is the hidden taxonomy used by WooCommerce to store the product type. You need to use view's hook wpv_filter_query in order to query the simple products.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/display-view-results-only-for-simple-product-type/#post-902918

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 3 voices.

Last updated by davidZ-4 6 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#902703

I am trying to: filter a view by woocommerce product type.

Link to a page where the issue can be seen: hidden link
you can see both simple and grouped product type in the view resulsts

I expected to see: only products with woocommerce type simple to show in the results

Instead, I got: all product types in the results

#902761

WooCommerce stores the Product type in a Taxonomy "product_type" which can natively have the following terms:

external
variable
grouped
simple

The problem is that this taxonomy is registered as follows in the WooCommerce Core:

'hierarchical'      => false,
					'show_ui'           => false,
					'show_in_nav_menus' => false,
					'query_var'         => is_admin(),
					'rewrite'           => false,
					'public'            => false,

Toolset Views cannot read this kind of taxonomy, which is hidden, on purpose, by WooCommerce.
The only closest you can get is this:
https://toolset.com/forums/topic/woocommerc-views-filter-by-product_type/#post-188378

To query by the WooCommerce taxonomy you would need to use custom code applied to the Views Hooks:
https://toolset.com/documentation/programmer-reference/views-filters/

#902768

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - as I understand, you only want to display 'simple" products with view - correct? If yes:

"product_type" is the hidden taxonomy used by WooCommerce to store the product type. You need to use view's hook wpv_filter_query in order to query the simple products.

For example - please add following code to your current theme's functions.php file:

add_filter( 'wpv_filter_query', 'func_filter_simple_products', 101, 3 );
function func_filter_simple_products( $query_args, $view_settings, $view_id ) {
     
    if ( $view_id == 9999) { // edit the id
 
        $query_args['tax_query'] = array(
          array(
            'taxonomy' => 'product_type',
            'field' => 'slug',
            'terms' => array('simple'),
            'operator' => 'IN'
          )
    }
 
    return $query_args;
}

Where:
- Replace 9999 with your original view ID.

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#902903

Hi Minesh,
thanks for the example of code.
I tried it but the PHP Syntax Check show an error in the code. not sure how to solve it. i tried to close the the first array brackets but I still get an error.

Any thoughts?

David

#902918

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Doh - there was a typo and I missed the semicolon (dont know how) 🙂

I corrected the code - could you please use following code - it should work:

add_filter( 'wpv_filter_query', 'func_filter_simple_products', 10, 3 );
function func_filter_simple_products( $query_args, $view_settings, $view_id ) {
      
    if ( $view_id == 9999) { // edit the id
  
        $query_args['tax_query'] = array(
          array(
            'taxonomy' => 'product_type',
            'field' => 'slug',
            'terms' => array('simple'),
            'operator' => 'IN'
          ));
    }
  
    return $query_args;
}

Where:
- Replace 9999 with your original view ID.

#902925

Thanks Minesh,

it works 🙂

David