Whenever we need to do custom WooCommerce work, remembering how to get “things” is the most time-consuming task. So, a Google search often helps us find the right and quickest solution.
For example, it’s easy to “get the current product’s categories“, but how can we get the “parent product categories” only? In today’s snippet we will see how this can be achieved in 7 lines of PHP. Enjoy!
![](https://www.businessbloomer.com/wp-content/uploads/2022/12/woocommerce-get-parent-categories-single-product-page-1024x534.png)
PHP Snippet: Get Current Product’s Parent Categories @ Single Product Page
The PHP below fills the $parent_categories array with parent product category IDs.
/**
* @snippet Get Parent Categories @ WooCommerce Single Product
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 7
* @community https://businessbloomer.com/club/
*/
$categories = get_the_terms( get_the_ID(), 'product_cat' );
$parent_categories = array();
foreach ( $categories as $category ) {
if ( $category->parent == 0 ) {
$parent_categories[] = $category->term_id;
}
}