Woocommerce – Work with product categories


<?php

/*
* Woocommerce – Work with categories
*/
if ( is_product_category() ){ // Condition for product category
global $wp_query;
$cat = $wp_query->get_queried_object();
$current_cat_id = $cat->term_id;
$cat_parent = $cat->parent; // get category parent

/* Get childrens of category */
$children = get_categories( array (‘taxonomy’ => ‘product_cat’, ‘parent’ => $current_cat_id ));

/*Get category link*/
$cat_link = get_category_link( $current_cat_id );

}

/*Get categories based on product id*/
$terms = get_the_terms( $post->ID, ‘product_cat’ );
foreach ($terms as $term) {
$product_cat = $term->name;
break;
}

?>

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s