从Woocommerce中的产品ID列出产品类别层次结构 [英] List product categories hierarchy from a product id in Woocommerce

查看:90
本文介绍了从Woocommerce中的产品ID列出产品类别层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在多个类别中使用的产品,示例请看以下字符串:

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

  • Cat1>Product1
  • Cat1>Product2
  • Cat2>subcat1>Product1
  • Cat3>subcat1>subcat2>Product1

在woocommerce中,如果我有productid,则可以执行以下操作:

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>');
}

但这只是返回产品ID的所有父类别。

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

cat1,cat2,subcat1,Cat3,subcat2

cat1, cat2, subcat1, Cat3, subcat2

我很难过。给我所需的产品ID,在上面的列表中构建-应该返回的是:

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

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

我基本上需要从产品ID重建每个类别路径。

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

推荐答案


要获取产品类别的所有祖先,可以使用Wordpress get_ancestors() 函数

对于给定产品的每个产品类别,以下自定义简码功能将输出,该产品类别的祖先是您的问题中定义的字符串:

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) . '"';
}

代码进入活动子主题(活动主题)的function.php文件。经过测试和工作。

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

使用情况:

1)在产品页面的php代码中:

1) In the php code of product page:

echo do_shortcode( "[product_cat_list]" );

2)在具有给定产品ID 的php代码中(此处产品ID为 37

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"


这篇关于从Woocommerce中的产品ID列出产品类别层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆