patrickgilmour
6/27/2014 - 12:47 AM

WooCommerce - show a product's single child category (term) from a defined Parent term in the WooCommerce Products taxonomy, 'product_cat'.

WooCommerce - show a product's single child category (term) from a defined Parent term in the WooCommerce Products taxonomy, 'product_cat'. Works on the Single Product Page.

<?php

/**
 * Show a Product's Child Category of a Parent 
 *
 * For example, A parent called "Brand" and a child called "Apple"
 */
add_action('woocommerce_after_single_product', 'my_woocommerce_after_single_product' );
function my_woocommerce_after_single_product () {

    global $product;

    $taxonomy = 'product_cat';
    $slug = 'brand'; //Or whatever the slug of "Brand" is

    $term = get_term_by('slug', $slug, $taxonomy);

    //Gets the ID of the Parent category
    $term_id = $term->term_id;

    //Get the Child terms
    $brand_terms = wp_get_post_terms( $product->id, $taxonomy, array("fields" => "all") );

        foreach ($brand_terms as $brand_item) {

            // Hunt out the child term that is a child of the parent
            if ( $brand_item->parent == $term_id ) {

                //Get the name of the brand
                $brand = $brand_item->name;

                break; //Assumes you've only assigned one Brand
            }

        }

        echo '<p>The Brand is: ' . $brand; //Apple or Microsoft or Google...
}

// see http://stackoverflow.com/questions/24033625/show-brand-on-woocommerce-product-page/24442399#24442399