I'm trying to add custom content before shop content but only when viewing products in specific category with this category childs.
Thoes categories are: main category - 'events', child categories - 'seminarium', 'course' and 'training'.
add_action( 'woocommerce_before_shop_loop', 'eventsTerms', 18);
function eventsTerms() {
$term = get_term_by('name', 'events', 'product_cat');
$term_id = $term->term_id;
$taxonomyName = 'product_cat';
$termChildren = get_term_children( $term_id, $taxonomyName );
foreach ( $termChildren as $child ) {
$_term = get_term_by( 'id', $child, $taxonomyName );
$childTerms= ', "' . '' . $_term->name . '"';
echo $childTerms; // only to see is ok
}
if ( is_product_category( array("events", $childTerms) ) ) {
global $product;
wp_list_categories(
array(
'show_option_all' => 'show all',
'taxonomy' => 'product_cat',
'style' => 'none',
'separator' => '',
'hide_empty' => 0,
));
}
}
$childTerms
returns all child categories names, so I want to use is_product_category
conditional tag with array, but my code still works only on main category "events".
Where's the bug?
EDIT 1
Ok, I thought the reason why it's not working is implode
can't be using in is_product_category()
args. So I was trying with json_encode
like that:
add_action( 'woocommerce_before_shop_loop', 'eventsTerms', 18);
function eventsTerms() {
$term = get_term_by('name', 'events', 'product_cat');
$term_id = $term->term_id;
$taxonomyName = 'product_cat';
$termChildren = get_term_children( $term_id, $taxonomyName );
foreach ( $termChildren as $child ) {
$_term = get_term_by( 'id', $child, $taxonomyName );
$childTerms[] = " '".$_term->name."'";
}
$x = implode(",", $childTerms);
$y = json_encode($x);
echo $y; // output as " 'seminarium', 'course', 'training'"
$y2 = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:', $x); // removing qutes by preg_replace
echo $y2; // output as 'seminarium', 'course', 'training'
if ( is_product_category( array('events', $y) ) ) {
global $product;
wp_list_categories(
array(
'show_option_all' => 0,
'taxonomy' => 'product_cat',
'style' => 'none',
'separator' => '',
'hide_empty' => 0,
'child_of' => $term_id,
));
}
}