I have a products that can be in multiple categories, example take a look at the following strings:
- Cat1>Product1
- Cat1>Product2
- Cat2>subcat1>Product1
- Cat3>subcat1>subcat2>Product1
In woocommerce, if I have a productid, I can do the following:
function alg_product_categories_names2( $atts ) {
$product_cats = get_the_terms( $this->get_product_or_variation_parent_id( $this->the_product ), 'product_cat' );
$cats = array();
$termstest= '';
if ( ! empty( $product_cats ) && is_array( $product_cats ) ) {
foreach ( $product_cats as $product_cat ) {
if ( $term->parent == 0 ) { //if it's a parent category
$termstest .= ' PARENTCAT= '. $product_cat->name;
}
}
}
return htmlentities('<categories_names>'. $termstest .'</categories_names>');
}
But this simply returns all the parent categories for the product id.
cat1, cat2, subcat1, Cat3, subcat2
I'm having a hard time with this. What I need is given a product id, build the list above - what should be returned is:
"Cat1>Product1" | "Cat2>subcat1>Product1" | "Cat3>subcat1>subcat2>Product1"
I basically need to rebuild each category path from the product id.