Last active
October 9, 2017 14:57
-
-
Save advokatb/9ead3559656db3f80d68aec2e385e711 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Display only children of current category in Woocommerce product categories widget */ | |
add_filter('woocommerce_product_categories_widget_args','woo_current_product_category'); | |
function woo_current_product_category( $args ){ | |
global $wp_query, $post, $woocommerce; | |
$include = array(); | |
$category_parent = ''; | |
$current_cat = ''; | |
if ( is_tax( 'product_cat' ) ) { | |
$cat_obj = $wp_query->get_queried_object(); | |
if( isset( $cat_obj->term_id ) ){ | |
$current_cat = $cat_obj; | |
$category_parent = $cat_obj->parent; | |
} | |
} elseif ( is_singular('product') ) { | |
$product_category = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'fields' => 'all' ) ); | |
$current_cat = end( $product_category ); | |
$category_parent = $current_cat->parent; | |
} | |
//check if current cat has children | |
if( ! empty( $current_cat ) ) | |
$current_cat_children = | |
get_terms( | |
'product_cat', | |
array( | |
'parent' => $current_cat->term_id, | |
'fields' => 'ids', | |
'hide_empty' => 0 | |
) | |
); | |
if( ! empty( $current_cat_children ) ){ | |
$terms_to_include = | |
array_merge( | |
array( $current_cat->term_id ), | |
$current_cat_children | |
); | |
}else{ | |
if( ! empty( $category_parent ) ){ | |
$terms_to_include = | |
array_merge( | |
array( $category_parent ), | |
get_terms( 'product_cat', array( 'parent' => $category_parent, 'fields' => 'ids', 'hide_empty' => 0 ) ) | |
); | |
} | |
} | |
if( ! empty( $terms_to_include ) ) | |
$include = $terms_to_include; | |
if( ! empty( $include ) ) | |
$args['include'] = $include; | |
return $args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment