【问题标题】:WooCommerce - get category for product pageWooCommerce - 获取产品页面的类别
【发布时间】:2013-02-24 12:44:26
【问题描述】:

对于我的 WC 产品页面,我需要向 body 标记添加一个类,以便我可以执行一些自定义样式。这是我为此创建的函数...

function my_add_woo_cat_class($classes) {

    $wooCatIdForThisProduct = "?????"; //help!

    // add 'class-name' to the $classes array
    $classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct;
    // return the $classes array
    return $classes;
}

//If we're showing a WC product page
if (is_product()) {
    // Add specific CSS class by filter
    add_filter('body_class','my_add_woo_cat_class');
}

...但是如何获得 WooCommerce 猫 ID?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    要将自定义类添加到正文标签,您可以使用body_class 挂钩。

    要在产品页面上添加一个更多类基于特定的产品类别您可以使用 Wordpress has_term 功能(检查产品是否属于该特定产品类别).

    为此,我创建了数组 $classes_to_add,其中:

    • key:可以是产品类别 id(或 slug 或名称)。或者它们的数组。 Read the documentation
    • value:包含要添加到 body 标记的类的字符串。如果要添加多个类,请创建一个包含多个值的字符串,并用空格分隔(请参见下面的示例)

    所以:

    // adds a class to the body element based on the product category
    add_filter( 'body_class', 'add_body_class_based_on_the_product_category' );
    function add_body_class_based_on_the_product_category( $classes ) {
    
        // only on the product page
        if ( ! is_product() ) {
            return $classes;
        }
    
        // create an array with the ids (or slugs) of the product categories and the respective class (or classes) to add
        $classes_to_add = array(
            30          => 'class-1',
            'cat_slug'  => 'class-2 class-3',
            32          => 'class-4 class-5',
        );
    
        // if the product belongs to one of the product categories in the array it adds the respective class (or classes)
        foreach ( $classes_to_add as $product_cat => $new_classes ) {
            if ( has_term( $product_cat, 'product_cat', get_the_ID() ) ) {
                 $classes[] = $new_classes;
            }
        }    
    
        return $classes;
    }
    

    代码已经过测试并且可以运行。将其添加到您的活动主题的 functions.php 中。

    【讨论】:

      【解决方案2】:
      <?php
         $terms = get_the_terms($product->ID, 'product_cat');
            foreach ($terms as $term) {
      
              $product_cat = $term->name;
                 echo $product_cat;
                   break;
        }
       ?>
      

      【讨论】:

      • 请提供并准确描述这如何帮助回答 OP 的问题。
      • 请阅读How to Answer - 描述你的代码。
      • 这与接受的答案非常相似,只是它回显类别名称而不是返回类别 ID!
      • @ban-geoengineering ok)
      【解决方案3】:

      $product-&gt;get_categories() 自 3.0 版起已弃用!请改用wc_get_product_category_list

      https://docs.woocommerce.com/wc-apidocs/function-wc_get_product_category_list.html

      【讨论】:

        【解决方案4】:

        我从位于我的主题目录的 woocommerce 文件夹中的 content-single-popup.php 中删除了这行代码。

        global $product; 
        echo $product->get_categories( ', ', ' ' . _n( ' ', '  ', $cat_count, 'woocommerce' ) . ' ', ' ' );
        

        由于我正在处理的主题已在其中集成了 woocommerce,因此这是我的解决方案。

        【讨论】:

        • 谢谢,返回的是类别名称,如何获取类别id,而不是名称?
        【解决方案5】:

        感谢信箱。我正在使用 MyStile 主题,我需要在搜索结果页面中显示产品类别名称。我将此功能添加到我的子主题functions.php

        希望对他人有所帮助。

        /* Post Meta */
        
        
        if (!function_exists( 'woo_post_meta')) {
            function woo_post_meta( ) {
                global $woo_options;
                global $post;
        
                $terms = get_the_terms( $post->ID, 'product_cat' );
                foreach ($terms as $term) {
                    $product_cat = $term->name;
                    break;
                }
        
        ?>
        <aside class="post-meta">
            <ul>
                <li class="post-category">
                    <?php the_category( ', ', $post->ID) ?>
                                <?php echo $product_cat; ?>
        
                </li>
                <?php the_tags( '<li class="tags">', ', ', '</li>' ); ?>
                <?php if ( isset( $woo_options['woo_post_content'] ) && $woo_options['woo_post_content'] == 'excerpt' ) { ?>
                    <li class="comments"><?php comments_popup_link( __( 'Leave a comment', 'woothemes' ), __( '1 Comment', 'woothemes' ), __( '% Comments', 'woothemes' ) ); ?></li>
                <?php } ?>
                <?php edit_post_link( __( 'Edit', 'woothemes' ), '<li class="edit">', '</li>' ); ?>
            </ul>
        </aside>
        <?php
            }
        }
        
        
        ?>
        

        【讨论】:

          【解决方案6】:

          一款 WC 产品可能不属于一个或多个 WC 类别。假设您只想获得一个 WC 类别 ID。

          global $post;
          $terms = get_the_terms( $post->ID, 'product_cat' );
          foreach ($terms as $term) {
              $product_cat_id = $term->term_id;
              break;
          }
          

          请查看 WooCommerce 插件的“templates/single-product/”文件夹中的 meta.php 文件。

          <?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
          

          【讨论】:

          • 刚刚将此代码添加到“price.php”,当访问单个产品页面时,我收到此错误消息:警告:为 foreach() 提供的参数无效。是我做错了什么还是这段代码与当前的 WC 版本不兼容?
          • 有没有办法用这个排除某些类别?
          • 这帮助我获得了产品信息,并在保存新评论后使用它重定向到类别列表。如果有任何帮助,这是我的问题。 stackoverflow.com/questions/42014311/…
          • 这太棒了。我什至将猫放在多个类别的数组中。很好的答案!
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-11
          • 1970-01-01
          • 2016-01-09
          • 2014-06-27
          • 1970-01-01
          • 2016-11-11
          相关资源
          最近更新 更多