简体   繁体   English

Woocommerce 在类别页面的产品名称旁边显示父类别

[英]Woocommerce show parent category next to product name on category page

We have our product categories set up as follow:我们的产品类别设置如下:

main category 1主要类别 1
-sub category 1.1 -子类别 1.1
-sub category 1.2 -子类别 1.2
-sub category 1.3 -子类别 1.3
main category 2主要类别 2
-sub category 2.1 -子类别 2.1
-sub category 2.2 -子类别 2.2
-sub category 2.3 -子类别 2.3
etc.等等

With the following code i am trying to display the parent category next to the product name on the category page.使用以下代码,我试图在类别页面上的产品名称旁边显示父类别。 What happens is that the category name of a product is only shown when a product is placed inside a parent category AND a sub category.发生的情况是产品的类别名称仅在产品放置在父类别和子类别中时显示。 Otherwise the name of the parent category isn't being displayed.否则不会显示父类别的名称。 For example;例如; if product 'A' has been placed inside parent category 1, there's no category name visible.如果产品“A”已放置在父类别 1 中,则没有可见的类别名称。 If i also place the product inside sub category 1.3, the parent category is visible.如果我还将产品放在子类别 1.3 中,则父类别是可见的。 How to solve this?如何解决这个问题?

//remove the Default Title and call another function which will show your own title in place
remove_action('woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title',10);
add_action('woocommerce_shop_loop_item_title','fun',10);
function fun()
{
    
    global $product;

// If the WC_product Object is not defined globally
if ( ! is_a( $product, 'WC_Product' ) ) {
    $product = wc_get_product( get_the_id() );
}
    
        //get product category  
       $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );

    if ( $product_cats && ! is_wp_error ( $product_cats ) ){

        //$single_cat = array_shift( $product_cats ); 
        ?>

        <!--<h2 itemprop="name" class="product_category_title"><span class="product_category"><?php //echo $single_cat->name; ?></span> |  <?php //echo $product->get_name();?></h2>!-->
        <h2 itemprop="name" class="product_category_title"><span class="product_category"><?php echo $product_cats[1]->name; ?></span> |  <?php echo $product->get_name();?></h2>



<?php }
}

I believe the problem is with this line我相信问题出在这条线上

<h2 itemprop="name" class="product_category_title"><span class="product_category"><?php echo $product_cats[1]->name; ?></span> |  <?php echo $product->get_name();?></h2>

you are using the second item in the array to display the name and when there is a subcategory this will be an array.您正在使用数组中的第二项来显示名称,当有子类别时,这将是一个数组。 And the categories in the array are not necessarily in order of parent subcategory.并且数组中的类别不一定按照父子类别的顺序。 So you need to loop through the result of $product_cats and determine which one is the parent before printing所以你需要循环遍历 $product_cats 的结果,并在打印之前确定哪一个是父级

//remove the Default Title and call another function which will show your own title in place
remove_action('woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title',10);
add_action('woocommerce_shop_loop_item_title','fun',10);
function fun()
{
    
    global $product;

    // If the WC_product Object is not defined globally
    if ( ! is_a( $product, 'WC_Product' ) ) {
        $product = wc_get_product( get_the_id() );
    }
    
    //get product category  
    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );

    if ( $product_cats && ! is_wp_error ( $product_cats ) ){
        
        foreach ( $product_cats as $product_cat ) {
            if ( $product_cat->parent == 0 ) {
        ?>

                <h2 itemprop="name" class="product_category_title"><span class="product_category"><?php echo $product_cat->name; ?></span> |  <?php echo $product->get_name();?></h2>
        <?php   
                break;
            }
        }
    }
}

This assumes that you will only ever have 1 main category assigned to a product这假设您只会将 1 个主要类别分配给产品

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM