Tell us what you are trying to do?List Product and Product variant form WooCommerce with out listing variant parent product in Toolset View & Search is not working after using the code
Is there any documentation that you are following? I a not able t find the documentation
Is there a similar example that we can see? no
What is the link to your site? hidden link
add_filter('wpv_filter_query', function($query_args, $view_settings, $view_id){ if($view_id == 4851){ $query_args['post_type'] = array('product_variation', 'product'); } return $query_args; }, 10, 3);
the above code is used to list the product and product variance in same view but I don't want to list parent product of variance to list in the view.
Hi Nelson,
Thank you for contacting us and I'd be happy to assist.
To make the view, list only the product variations ( and not the Products ), you can update the code line, from:
$query_args['post_type'] = array('product_variation', 'product');
To:
$query_args['post_type'] = array('product_variation');
I hope this helps and please let me know how it goes.
regards,
Waqar
How I want is list product with out variations and also the single variation product. So I don't want to list the parent product of variation. please see the attachment I don't want to display the crossed products
Hi Nelson,
Thanks for writing back and for sharing the screenshot.
To exclude the products which have a variation, you'll need to extend your custom function to first get the list of all the product posts and then exclude those, which have a variation.
For example:
add_filter('wpv_filter_query', function($query_args, $view_settings, $view_id){
if($view_id == 4851){
// get the list of all published products
$products_args = array(
'numberposts' => -1,
'post_type' => 'product'
);
$all_products = get_posts( $products_args );
// check if a product has any variations
foreach ($all_products as $single_product) {
$product = new WC_Product_Variable( $single_product->ID );
$variations = $product->get_available_variations();
if(sizeof($variations) >= 1) {
// if product has a variation, include it in the posts to exclude list
$query_args['post__not_in'][] = $single_product->ID;
}
}
// set the required post type
$query_args['post_type'] = array('product_variation', 'product');
}
return $query_args; }, 10, 3);
I hope this helps and for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar