I am developing a WooCommerce theme, and on single product page I want to display a carousel of products from same vendor category.
I have the following category structure:
All products
- product category no 1
- product category no 2
- product cat ...
Vendors
- vendor name 1
- vendor name 2
- vendor name ...
While viewing product from category no 1 I would like to display a carousel off other products from same vendor name category if product is assigned to such child category.
In my example Vendors and All products are parent categories on the same level.
Doing a custom product loop is quiet easy, but to be honest I have no idea how I should handle the logic behind finding the right child category. I just can't imagine it , and that's why I can't came up with the right solution.
Each product can be assigned only to one vendor name, and both vendors and products are regular woocommerce categories here.
For the time being I have the following code
wooctrl_same_vendor_products('vendors');
function wooctrl_same_vendor_products($parent_cat_name){
$parentCat = get_term_by('name', $parent_cat_name, 'product_cat');
$product_cat_ID = $parentCat->term_id;
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $product_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
foreach ($subcats as $subcat) {
//check if product is inside ?
}
}
Should I first create an array of all Vendors child categories and the search for product there or is there a cleaner way to solve it directly from the $product variable?