Get current product category object in WooCommerce

/**
 * Gets current product category object.
 *
 * `$current_category` is a `WP_Term` object, e.g.:
 * - `$current_category->term_id`
 * - `$current_category->name`
 * - `$current_category->slug`
 *
 * @see https://wpcodebook.com/get-current-product-category-woocommerce/
 * @see https://developer.wordpress.org/reference/functions/get_queried_object/
 *
 * @return WP_Term|false
 */
function wpcodebook_get_current_product_category() {
	if (
		is_product_category() &&
		( $current_category = get_queried_object() )
	) {
		return $current_category;
	}
	return false;
}

Leave a Comment