List product categories hierarchy from a product id in Woocommerce

北城余情 提交于 2019-12-10 23:33:13

问题


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.


回答1:


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('" | "', $output) . '"';
}

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"


来源:https://stackoverflow.com/questions/53312319/list-product-categories-hierarchy-from-a-product-id-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!