Search code examples
woocommerceparentcategories

Woocommerce get parent slug / name on single-product.php


I know that people have asked before on here regarding woocommerce and getting the parent category. I've done quite a bit of research through Stack Overflow, and seen quite a lot of answers that don't fit the simplicity of what I'm trying to achieve.

Basically I have my single-product.php page, and we have a site that requires different headers / footer and design based on what category a product belongs in.

To achieve this we just need to access the name of the parent category so we can dictate what header / footer it should load.

I did find this on wordpress.stackexchange, but it's too complex for my needs, so I tried stripping some of the code out and placing directly in the single-product.php page like this:

global $post;

$descendant = get_the_terms( $post->ID, 'product_cat' );
$descendant = array_reverse($descendant);
$descendant = $descendant[0];
$descendant_id = $descendant->term_id;
$ancestors = array_reverse(get_ancestors($descendant_id, 'product_cat'));
$ac = count($ancestors);
$c = 1;
$origin_ancestor_term = get_term_by("id", $ancestors[0], "product_cat");
$origin_ancestor_term->name;

$headername = $origin_ancestor_term->name;


if ($headername == 'Events') {
    $productparentcat = 'events';
} else {
    $productparentcat = 'adventures';
}

get_header($productparentcat);

Strangely this did seem to work on my local version until I updated the version of wordpress and then it broke. I also get the following errors now:

Notice: Undefined offset: 0 (for $c = 1;)
Notice: Trying to get property of non-object (for the $origin_ancestor_term->name;)

Does anyone know an easy way to just retrieve the parent category name on the single-product template? I've looked a lot of standard wordpress functions to achieve this, but they don't work with woocommerce templates, I guess because they use a custom taxonomy...

Thank you!


Solution

  • After further researching the problem, I realised the answer was ridiculously simple and that the main issue was I was looking for a solution in the wrong place.

    I'd become fixated with the idea of finding the products' category parent ID / name, but in fact all the products on the site were included under both child and parent categories.

    So for example, a product called "Fiesta Special Edition" was under both "Fiesta" and "Ford", so if I had tried to search for a parent for the category "Fiesta" using the product ID it didn't work, since it was looking for a parent for "Ford" as well and that was the top most level category.

    This was what caused my Error regarding "Notice: Undefined offset: 0 (for $c = 1;)".

    In the end I used this:

    if ( has_term('Ford', 'product_cat', null) ) {
        get_header('ford');
    } else {
        get_header('other-makes');
    }
    
    
    
    $carclass = '';
    
    if(has_term('Ford', 'product_cat', $post)) {
        $carclass = 'ford';
    } else {
        $carclass = 'other-cars';
    }
    

    That allowed me to style the page and pull in the right header depending on if a specific category was ticked.