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?
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.
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?
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
Thank you for the suggested code, Luo. I'll try it as soon as I can find the relevant view_id ...
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?
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.
My issue is resolved now. Thank you!