Skip Navigation

[Resolved] List Product and Product variant

This support ticket is created 5 years, 4 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Waqar 5 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#1291379

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

#1291391

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.

#1291703

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

#1291759
toolset.jpg

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

#1291917

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