Skip to content

Instantly share code, notes, and snippets.

@shubhw12
Created June 24, 2020 14:57
Show Gist options
  • Save shubhw12/8f15f64e97079e901843d1788784f48f to your computer and use it in GitHub Desktop.
Save shubhw12/8f15f64e97079e901843d1788784f48f to your computer and use it in GitHub Desktop.
Display parent category below individual product if only we are showing only products on the shop page.
add_filter('astra_woo_shop_product_categories' , 'fnc_category');
function fnc_category($args = array()){
$woocommerce_category_id = get_queried_object_id();
$args = array(
'parent' => $woocommerce_category_id
);
$terms = get_terms( 'product_cat', $args );
$categories = array();
if ( $terms ) {
echo '<ul class="woocommerce-categories">';
foreach ( $terms as $term ) {
array_push($categories , $term-> name);
}
}
global $product;
$category = get_the_category($product->id);
$product_categories = function_exists( 'wc_get_product_category_list' ) ? wc_get_product_category_list( get_the_ID(), ';', '', '' ) : $product->get_categories( ';', '', '' );
$product_categories = htmlspecialchars_decode( wp_strip_all_tags( $product_categories ) );
$pro_categories = explode( ';', $product_categories );
foreach($categories as $category){
foreach($pro_categories as $pro_category){
if( $category == $pro_category){
$final_category = $category;
}
}
}
return $final_category;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment