Skip Navigation

[Resolved] Relationship between posts from different categories of WooCommerce Product CPT

This support ticket is created 4 years, 10 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/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by Anthony 4 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1517057

I'm building a audio files shop site using Toolset custom fields to augment the WooCommerce Product CPT. I'm also using the WooCommerce extension Product Bundles to bundle together single track products into album products. I would like to be able to display data from custom fields in the track product posts on the single product page of the album product those tracks are on, so I want to be able to create relationships between those posts.

Last July, in response to another user's support request regarding relationships between posts of the same type (for a genealogy site), it was mentioned that this is "on the roadmap". What's possible with Toolset and WooCommerce is already pretty awesome - what about support for same-CPT relationships with WooCommerce Product Bundles?

#1517797

Hello,

For the question:
what about support for same-CPT relationships with WooCommerce Product Bundles?
I assume you are going to setup many-to-many relationship between posts of same post type.
If it is, there isn't such kind of built-in feature yet.

For the plugin "WooCommerce extension Product Bundles", it depends on how does this plugin setup the "Bundles" relationships.

If it is using Woocommerce built-in group products feature, then it is possible with some custom codes:
The Woocommerce group product is using a hidden field "_children" to store related products post IDs as an array, so you can use it to pass to Views query by filter hook wpv_filter_query, see our document:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

And WordPress document:
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
post__in (array) – use post ids. Specify posts to retrieve.

#1518101

Thank you, Luo, for your assistance.

As the WooCommerce Product Bundles is an official WooCommerce product, I'm assuming that it is using the hidden field "_children" as you've suggested. I have looked at the documentation that you've pointed me to, but the example code provided for wpv_filter_query seems very different from my situation/need, and I'm out of my depth about how to properly construct the necessary code to filter the View I want. Can you provide any further clues for me?

#1518181
grouped-product.JPG

For example, there are grouped products in your website:
- Grouped product A
-- Subproduct B
-- Subproduct C
See screenshot grouped-product.JPG

You can try these:
1) Create a post view:
- Query product posts
- Without any filters
- In view's loop, display product post information

2) Add below custom PHP codes in your theme file "functions.php":

add_filter('wpv_filter_query', function($query, $setting, $view_id){
	if($view_id == 123){
		$product_id = get_the_ID();
		$childen = get_post_meta($product_id, '_children');
		if(isset($childen[0])){
			$query['post__in'] = $childen[0];
		}
		else{
			$query['post__in'] = array(0);
		}
	}
	return $query;
}, 10, 3);

replace 123 with your post view's ID

3) Display above post view in the product post content, it should be able to display below in Grouped product A:
-- Subproduct B
-- Subproduct C

And display below in "Subproduct B" and "Subproduct C":
No items found

#1519215

Thank you for the suggested code, Luo. I'll try it as soon as I can find the relevant view_id ...

#1519795

Hi again, Luo. Once I determined the appropriate view_id and tried your code, I found it didn't produce any result. I decided to take a look in the database at the wp_post and wp_postmeta records for the relevant WooCommerce products and determined that '__children' is not what's being used to associate Product Bundles. Looking further at the database, I discovered the wp_woocommerce_bundled_items table and determined the two relevant columns in the records in there: product_id (the post_ids of the bundled products) and bundle_id (the post_id of the product bundle).

So, to display the list of bundled products in a View in the Content Template for the single product view of a particular product bundle, I need to loop through the wp_woocommerce_bundled_items table looking for the records where bundle_id equals the post_id of the product bundle, and fetch the product posts whose post_id equals each found record's product_id . I wonder if you can provide me with some guidance on how to accomplish that?

#1519889

The codes I provided above is for Woocommerce built-in group products feature, the database table "wp_woocommerce_bundled_items" is not a WordPress database table, so it is out the range of Toolset support.

I suggest you check it with the plugin author of "WooCommerce extension Product Bundles", check how to get the related Product post IDs, then pass them to View's query.

#1519893

My issue is resolved now. Thank you!