3

I'm working with the woo commerece plugin and i'd like to have a sub heading under the title of each product. Style and format is sorted however i want a particular Category to show in the sub heading section. I've managed to get as far as showing all categories but i want to narrow this down to just one category that is under a parent category. Below is the code i am using, could anyone suggest how i could achieve showing any child category selected under a parent category. Thanks

<?php
/**
 * Single Product title
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $post, $product;

$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
?>

<h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1>

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Artist:', 'Artist:', $cat_count, 'woocommerce' ) . ' ', '.</span>' ); ?>

This is what came out:

Array ( [0] => stdClass Object ( [term_id] => 59 [name] => Colourful [slug] => colourful [term_group] => 0 [term_taxonomy_id] => 59 [taxonomy] => product_cat [description] => [parent] => 115 [count] => 21 [filter] => raw ) [1] => stdClass Object ( [term_id] => 96 [name] => Karen Grant [slug] => karen-grant [term_group] => 0 [term_taxonomy_id] => 96 [taxonomy] => product_cat [description] => [parent] => 90 [count] => 5 [filter] => raw ) [2] => stdClass Object ( [term_id] => 69 [name] => Landscapes [slug] => landscapes [term_group] => 0 [term_taxonomy_id] => 69 [taxonomy] => product_cat [description] => [parent] => 115 [count] => 35 [filter] => raw ) [3] => stdClass Object ( [term_id] => 71 [name] => Nature [slug] => nature [term_group] => 0 [term_taxonomy_id] => 71 [taxonomy] => product_cat [description] => [parent] => 115 [count] => 20 [filter] => raw ) )

<?php

/**
 * Single Product title
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly


global $post, $product;

$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
?>


<h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1>

<?php 


    global $post, $product;

    $cat_array = array();
    $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details

    foreach($term_list as $cat_list)
    {

        array_push($cat_array, $cat_list->term_id);

    }

    $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array

   $termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1

   $final_result = array_intersect($cat_array,$termchildren); print_r($final_result);

    $new_cat_id = $final_result[0];

    $cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID

    $term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID

    $name = $term->name; //Store it into an varialbe

    echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
?>

'

4
  • I didn't get you.Can you please elaborate more ? Commented Dec 31, 2014 at 5:05
  • I have a products on my site and i'd like to add the artist of the product under the product title. I've done this but i want to display the artist name. Each artist is also a category, so i would like to display categories linked to this product however i have other categories linked to this. I would like to only allow the parent category of the artists to be displayed which would eliminate all other categories. The parent category is Artists, is there a way to say product category artists so that it doesn't display all categories only the child from artists. Commented Dec 31, 2014 at 10:21
  • I am still not getting it :) But what I got is, you want to have one name only below the title which is Artist.So if this is the case, I would suggest you to do with custom field and retrieve it. Commented Dec 31, 2014 at 10:38
  • Thats right but i want to pull this name from the category. The above code does this but pulls all categories linked to that product. Is there away to specifically state only pull categories under a parent category? instead of looking at all categories? Commented Dec 31, 2014 at 10:43

1 Answer 1

4

Try this :

<?php 

    global $post, $product;

    $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details

    $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array

    $cat_url = get_term_link ($cat_id, 'product_cat'); //get link of parent ID

    $term = get_term( $cat_id, 'product_cat' ); //Get Name of the parent from the parent ID

    $name = $term->name; //Store it into an varialbe

    echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";

?>

Remember :

In WooCommerce, products categories are not normal categories, they are a custom taxonomy created specifically for products which is just labeled as "Categories".

Let me know If you have any doubt.

Updated:

    <?php 

        global $post, $product;

        $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details

        $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array

       $termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1

        $new_cat_id = $termchildren[2];

        $cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID

        $term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID

        $name = $term->name; //Store it into an varialbe

        echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";

    ?>

New Updated(02 January 2015)

    <?php 

        global $post, $product;

        $cat_array = array();
        $term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details

        foreach($term_list as $cat_list)
        {

            array_push($cat_array, $cat_list->term_id);

        }

        $cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array

       $termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1

       $final_result = array_intersect($cat_array,$termchildren);

       $final_cat_result = array_values($final_result);

        $new_cat_id = $final_cat_result[0];

        $cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID

        $term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID

        $name = $term->name; //Store it into an varialbe

        echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
    ?>

Line : 1 $post and $product both are the global variables.So in term to use that in other files, we need to add that in our file before using it.

Line : 2 One blank array to store all the categories of the current product**.We will use it in a future.

Line : 3 wp_get_post_terms is used to retrieve the terms of the post(for woocommerce its category for product).So now we have an array containing all the details of terms with ID, name etc etc

Line : 4 It is for the loop through above generated array.We will loop through an array and look for term_id.We will use array_push to store all the term ID and for storing we will use blank array from line 2.So now we have an array of term_id.

Line : 9 Now we will use get_term_children to retrieve the children term of Artist as we know the artist term ID and its fixed.It will give an array as an output.

Line : 10 array_intersect is useful to match two array and fetch out only matching values.(Basically we are looking in to the current product category and all artist category to take out the matching category only).

Line : 11 array_values is useful to re-index the array.(By adding this line we solve the error that was coming :) )

Line : 12 Now we have an array that is having only one value which is artist's term ID.(That's it.Now you need to fetch only name and link of the artist from that term ID)

Line : 13 Fetch link of the artist.

Line : 15 Fetch name of the artist from an array generated in line 14 and store it in variable.

Line : 16 Print the needed thing and we are done !

Sign up to request clarification or add additional context in comments.

28 Comments

Would it be easier for you to see the site? I can open access now. The product i am working off as an example is: artgiftedbygod.co.uk/online-art-shop/conceptual/repeated-11
Those numbers generated seem to be the sub cats under the other parent cat
So you always want to use artist(whose ID is 90) as the parent and then want to retrieve its child..right?
Yep. So basically the user sees the title which is the art work title and then under this the artist is listed, so they know who painted the work.
So if you manually add 90 $termchildren = get_term_children( 90, 'product_cat' ); and then do print_r($termchildren) what is it giving? Do the change and let me know that generated array is having your ID that you want or not.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.