List product categories hierarchy from a product id in Woocommerce










1















I have a products that can be in multiple categories, example take a look at the following strings:



  • Cat1>Product1

  • Cat1>Product2

  • Cat2>subcat1>Product1

  • Cat3>subcat1>subcat2>Product1

In woocommerce, if I have a productid, I can do the following:



 function alg_product_categories_names2( $atts ) 
$product_cats = get_the_terms( $this->get_product_or_variation_parent_id( $this->the_product ), 'product_cat' );
$cats = array();

$termstest= '';
if ( ! empty( $product_cats ) && is_array( $product_cats ) )
foreach ( $product_cats as $product_cat )
if ( $term->parent == 0 ) //if it's a parent category
$termstest .= ' PARENTCAT= '. $product_cat->name;




return htmlentities('<categories_names>'. $termstest .'</categories_names>');



But this simply returns all the parent categories for the product id.



cat1, cat2, subcat1, Cat3, subcat2



I'm having a hard time with this. What I need is given a product id, build the list above - what should be returned is:



"Cat1>Product1" | "Cat2>subcat1>Product1" | "Cat3>subcat1>subcat2>Product1"



I basically need to rebuild each category path from the product id.










share|improve this question
























  • As a new user welcome… You should take quick tour (30 seconds) to better understand basically how things work on StackOverFlow.

    – LoicTheAztec
    Nov 15 '18 at 13:52
















1















I have a products that can be in multiple categories, example take a look at the following strings:



  • Cat1>Product1

  • Cat1>Product2

  • Cat2>subcat1>Product1

  • Cat3>subcat1>subcat2>Product1

In woocommerce, if I have a productid, I can do the following:



 function alg_product_categories_names2( $atts ) 
$product_cats = get_the_terms( $this->get_product_or_variation_parent_id( $this->the_product ), 'product_cat' );
$cats = array();

$termstest= '';
if ( ! empty( $product_cats ) && is_array( $product_cats ) )
foreach ( $product_cats as $product_cat )
if ( $term->parent == 0 ) //if it's a parent category
$termstest .= ' PARENTCAT= '. $product_cat->name;




return htmlentities('<categories_names>'. $termstest .'</categories_names>');



But this simply returns all the parent categories for the product id.



cat1, cat2, subcat1, Cat3, subcat2



I'm having a hard time with this. What I need is given a product id, build the list above - what should be returned is:



"Cat1>Product1" | "Cat2>subcat1>Product1" | "Cat3>subcat1>subcat2>Product1"



I basically need to rebuild each category path from the product id.










share|improve this question
























  • As a new user welcome… You should take quick tour (30 seconds) to better understand basically how things work on StackOverFlow.

    – LoicTheAztec
    Nov 15 '18 at 13:52














1












1








1








I have a products that can be in multiple categories, example take a look at the following strings:



  • Cat1>Product1

  • Cat1>Product2

  • Cat2>subcat1>Product1

  • Cat3>subcat1>subcat2>Product1

In woocommerce, if I have a productid, I can do the following:



 function alg_product_categories_names2( $atts ) 
$product_cats = get_the_terms( $this->get_product_or_variation_parent_id( $this->the_product ), 'product_cat' );
$cats = array();

$termstest= '';
if ( ! empty( $product_cats ) && is_array( $product_cats ) )
foreach ( $product_cats as $product_cat )
if ( $term->parent == 0 ) //if it's a parent category
$termstest .= ' PARENTCAT= '. $product_cat->name;




return htmlentities('<categories_names>'. $termstest .'</categories_names>');



But this simply returns all the parent categories for the product id.



cat1, cat2, subcat1, Cat3, subcat2



I'm having a hard time with this. What I need is given a product id, build the list above - what should be returned is:



"Cat1>Product1" | "Cat2>subcat1>Product1" | "Cat3>subcat1>subcat2>Product1"



I basically need to rebuild each category path from the product id.










share|improve this question
















I have a products that can be in multiple categories, example take a look at the following strings:



  • Cat1>Product1

  • Cat1>Product2

  • Cat2>subcat1>Product1

  • Cat3>subcat1>subcat2>Product1

In woocommerce, if I have a productid, I can do the following:



 function alg_product_categories_names2( $atts ) 
$product_cats = get_the_terms( $this->get_product_or_variation_parent_id( $this->the_product ), 'product_cat' );
$cats = array();

$termstest= '';
if ( ! empty( $product_cats ) && is_array( $product_cats ) )
foreach ( $product_cats as $product_cat )
if ( $term->parent == 0 ) //if it's a parent category
$termstest .= ' PARENTCAT= '. $product_cat->name;




return htmlentities('<categories_names>'. $termstest .'</categories_names>');



But this simply returns all the parent categories for the product id.



cat1, cat2, subcat1, Cat3, subcat2



I'm having a hard time with this. What I need is given a product id, build the list above - what should be returned is:



"Cat1>Product1" | "Cat2>subcat1>Product1" | "Cat3>subcat1>subcat2>Product1"



I basically need to rebuild each category path from the product id.







php wordpress woocommerce product custom-taxonomy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:31









LoicTheAztec

91.2k1365105




91.2k1365105










asked Nov 15 '18 at 4:10









mrDmrD

153




153












  • As a new user welcome… You should take quick tour (30 seconds) to better understand basically how things work on StackOverFlow.

    – LoicTheAztec
    Nov 15 '18 at 13:52


















  • As a new user welcome… You should take quick tour (30 seconds) to better understand basically how things work on StackOverFlow.

    – LoicTheAztec
    Nov 15 '18 at 13:52

















As a new user welcome… You should take quick tour (30 seconds) to better understand basically how things work on StackOverFlow.

– LoicTheAztec
Nov 15 '18 at 13:52






As a new user welcome… You should take quick tour (30 seconds) to better understand basically how things work on StackOverFlow.

– LoicTheAztec
Nov 15 '18 at 13:52













1 Answer
1






active

oldest

votes


















0















To get all ancestors of a product category, you can use the Wordpress get_ancestors() function




The following custom shortcode function will output for each product category of a given product, the ancestors with the product category in a string as defined in your question:



add_shortcode( 'product_cat_list', 'list_product_categories' )
function list_product_categories( $atts )
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'product_cat_list' );

$output = ; // Initialising
$taxonomy = 'product_cat'; // Taxonomy for product category

// Get the product categories terms ids in the product:
$terms_ids = wp_get_post_terms( $atts['id'], $taxonomy, array('fields' => 'ids') );

// Loop though terms ids (product categories)
foreach( $terms_ids as $term_id )
$term_names = ; // Initialising category array

// Loop through product category ancestors
foreach( get_ancestors( $term_id, $taxonomy ) as $ancestor_id )
// Add the ancestors term names to the category array
$term_names = get_term( $ancestor_id, $taxonomy )->name;

// Add the product category term name to the category array
$term_names = get_term( $term_id, $taxonomy )->name;

// Add the formatted ancestors with the product category to main array
$output = implode(' > ', $term_names);

// Output the formatted product categories with their ancestors
return '"' . implode('"


Code goes in function.php file of your active child theme (active theme). Tested and works.




USAGE:



1) In the php code of product page:



echo do_shortcode( "[product_cat_list]" );


2) In php code with a given product ID (here the product ID is 37 for example):



echo do_shortcode( "[product_cat_list id='37']" );



I think that the product name is not needed in your output as it is repetitive (on each product category). So you will get something like this:



"Cat1" | "Cat2>subcat1" | "Cat3>subcat1>subcat2"






share|improve this answer

























  • WOW. Thanks for the great answer. I got it working.

    – mrD
    Nov 15 '18 at 13:39










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312319%2flist-product-categories-hierarchy-from-a-product-id-in-woocommerce%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















To get all ancestors of a product category, you can use the Wordpress get_ancestors() function




The following custom shortcode function will output for each product category of a given product, the ancestors with the product category in a string as defined in your question:



add_shortcode( 'product_cat_list', 'list_product_categories' )
function list_product_categories( $atts )
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'product_cat_list' );

$output = ; // Initialising
$taxonomy = 'product_cat'; // Taxonomy for product category

// Get the product categories terms ids in the product:
$terms_ids = wp_get_post_terms( $atts['id'], $taxonomy, array('fields' => 'ids') );

// Loop though terms ids (product categories)
foreach( $terms_ids as $term_id )
$term_names = ; // Initialising category array

// Loop through product category ancestors
foreach( get_ancestors( $term_id, $taxonomy ) as $ancestor_id )
// Add the ancestors term names to the category array
$term_names = get_term( $ancestor_id, $taxonomy )->name;

// Add the product category term name to the category array
$term_names = get_term( $term_id, $taxonomy )->name;

// Add the formatted ancestors with the product category to main array
$output = implode(' > ', $term_names);

// Output the formatted product categories with their ancestors
return '"' . implode('"


Code goes in function.php file of your active child theme (active theme). Tested and works.




USAGE:



1) In the php code of product page:



echo do_shortcode( "[product_cat_list]" );


2) In php code with a given product ID (here the product ID is 37 for example):



echo do_shortcode( "[product_cat_list id='37']" );



I think that the product name is not needed in your output as it is repetitive (on each product category). So you will get something like this:



"Cat1" | "Cat2>subcat1" | "Cat3>subcat1>subcat2"






share|improve this answer

























  • WOW. Thanks for the great answer. I got it working.

    – mrD
    Nov 15 '18 at 13:39















0















To get all ancestors of a product category, you can use the Wordpress get_ancestors() function




The following custom shortcode function will output for each product category of a given product, the ancestors with the product category in a string as defined in your question:



add_shortcode( 'product_cat_list', 'list_product_categories' )
function list_product_categories( $atts )
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'product_cat_list' );

$output = ; // Initialising
$taxonomy = 'product_cat'; // Taxonomy for product category

// Get the product categories terms ids in the product:
$terms_ids = wp_get_post_terms( $atts['id'], $taxonomy, array('fields' => 'ids') );

// Loop though terms ids (product categories)
foreach( $terms_ids as $term_id )
$term_names = ; // Initialising category array

// Loop through product category ancestors
foreach( get_ancestors( $term_id, $taxonomy ) as $ancestor_id )
// Add the ancestors term names to the category array
$term_names = get_term( $ancestor_id, $taxonomy )->name;

// Add the product category term name to the category array
$term_names = get_term( $term_id, $taxonomy )->name;

// Add the formatted ancestors with the product category to main array
$output = implode(' > ', $term_names);

// Output the formatted product categories with their ancestors
return '"' . implode('"


Code goes in function.php file of your active child theme (active theme). Tested and works.




USAGE:



1) In the php code of product page:



echo do_shortcode( "[product_cat_list]" );


2) In php code with a given product ID (here the product ID is 37 for example):



echo do_shortcode( "[product_cat_list id='37']" );



I think that the product name is not needed in your output as it is repetitive (on each product category). So you will get something like this:



"Cat1" | "Cat2>subcat1" | "Cat3>subcat1>subcat2"






share|improve this answer

























  • WOW. Thanks for the great answer. I got it working.

    – mrD
    Nov 15 '18 at 13:39













0












0








0








To get all ancestors of a product category, you can use the Wordpress get_ancestors() function




The following custom shortcode function will output for each product category of a given product, the ancestors with the product category in a string as defined in your question:



add_shortcode( 'product_cat_list', 'list_product_categories' )
function list_product_categories( $atts )
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'product_cat_list' );

$output = ; // Initialising
$taxonomy = 'product_cat'; // Taxonomy for product category

// Get the product categories terms ids in the product:
$terms_ids = wp_get_post_terms( $atts['id'], $taxonomy, array('fields' => 'ids') );

// Loop though terms ids (product categories)
foreach( $terms_ids as $term_id )
$term_names = ; // Initialising category array

// Loop through product category ancestors
foreach( get_ancestors( $term_id, $taxonomy ) as $ancestor_id )
// Add the ancestors term names to the category array
$term_names = get_term( $ancestor_id, $taxonomy )->name;

// Add the product category term name to the category array
$term_names = get_term( $term_id, $taxonomy )->name;

// Add the formatted ancestors with the product category to main array
$output = implode(' > ', $term_names);

// Output the formatted product categories with their ancestors
return '"' . implode('"


Code goes in function.php file of your active child theme (active theme). Tested and works.




USAGE:



1) In the php code of product page:



echo do_shortcode( "[product_cat_list]" );


2) In php code with a given product ID (here the product ID is 37 for example):



echo do_shortcode( "[product_cat_list id='37']" );



I think that the product name is not needed in your output as it is repetitive (on each product category). So you will get something like this:



"Cat1" | "Cat2>subcat1" | "Cat3>subcat1>subcat2"






share|improve this answer
















To get all ancestors of a product category, you can use the Wordpress get_ancestors() function




The following custom shortcode function will output for each product category of a given product, the ancestors with the product category in a string as defined in your question:



add_shortcode( 'product_cat_list', 'list_product_categories' )
function list_product_categories( $atts )
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'product_cat_list' );

$output = ; // Initialising
$taxonomy = 'product_cat'; // Taxonomy for product category

// Get the product categories terms ids in the product:
$terms_ids = wp_get_post_terms( $atts['id'], $taxonomy, array('fields' => 'ids') );

// Loop though terms ids (product categories)
foreach( $terms_ids as $term_id )
$term_names = ; // Initialising category array

// Loop through product category ancestors
foreach( get_ancestors( $term_id, $taxonomy ) as $ancestor_id )
// Add the ancestors term names to the category array
$term_names = get_term( $ancestor_id, $taxonomy )->name;

// Add the product category term name to the category array
$term_names = get_term( $term_id, $taxonomy )->name;

// Add the formatted ancestors with the product category to main array
$output = implode(' > ', $term_names);

// Output the formatted product categories with their ancestors
return '"' . implode('"


Code goes in function.php file of your active child theme (active theme). Tested and works.




USAGE:



1) In the php code of product page:



echo do_shortcode( "[product_cat_list]" );


2) In php code with a given product ID (here the product ID is 37 for example):



echo do_shortcode( "[product_cat_list id='37']" );



I think that the product name is not needed in your output as it is repetitive (on each product category). So you will get something like this:



"Cat1" | "Cat2>subcat1" | "Cat3>subcat1>subcat2"







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 13:39

























answered Nov 15 '18 at 13:02









LoicTheAztecLoicTheAztec

91.2k1365105




91.2k1365105












  • WOW. Thanks for the great answer. I got it working.

    – mrD
    Nov 15 '18 at 13:39

















  • WOW. Thanks for the great answer. I got it working.

    – mrD
    Nov 15 '18 at 13:39
















WOW. Thanks for the great answer. I got it working.

– mrD
Nov 15 '18 at 13:39





WOW. Thanks for the great answer. I got it working.

– mrD
Nov 15 '18 at 13:39



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312319%2flist-product-categories-hierarchy-from-a-product-id-in-woocommerce%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3