douchui3933 2019-08-09 22:45
浏览 103
已采纳

针对特定产品类别,它是WooCommerce中的孩子

I'm trying to add custom content before shop content but only when viewing products in specific category with this category childs.

Thoes categories are: main category - 'events', child categories - 'seminarium', 'course' and 'training'.

add_action( 'woocommerce_before_shop_loop', 'eventsTerms', 18);
function eventsTerms() {

  $term = get_term_by('name', 'events', 'product_cat');
  $term_id = $term->term_id;
  $taxonomyName = 'product_cat';
  $termChildren = get_term_children( $term_id, $taxonomyName );

foreach ( $termChildren as $child ) {
  $_term = get_term_by( 'id', $child, $taxonomyName );
  $childTerms= ', "' . '' . $_term->name . '"';

  echo $childTerms; // only to see is ok

}

    if ( is_product_category( array("events", $childTerms) ) ) {
    global $product;
    wp_list_categories( 
      array(
          'show_option_all' => 'show all',
          'taxonomy' => 'product_cat',
          'style' => 'none',
          'separator' => '',
          'hide_empty' => 0,
      ));

  }

}

$childTerms returns all child categories names, so I want to use is_product_category conditional tag with array, but my code still works only on main category "events".

Where's the bug?

EDIT 1

Ok, I thought the reason why it's not working is implode can't be using in is_product_category() args. So I was trying with json_encode like that:

add_action( 'woocommerce_before_shop_loop', 'eventsTerms', 18);
function eventsTerms() {

  $term = get_term_by('name', 'events', 'product_cat');
  $term_id = $term->term_id;
  $taxonomyName = 'product_cat';
  $termChildren = get_term_children( $term_id, $taxonomyName );

foreach ( $termChildren as $child ) {
  $_term = get_term_by( 'id', $child, $taxonomyName );
  $childTerms[] = " '".$_term->name."'";
}

$x = implode(",", $childTerms);

$y = json_encode($x);

echo $y; // output as " 'seminarium', 'course', 'training'"

$y2 = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:', $x); // removing qutes by preg_replace

echo $y2; // output as 'seminarium', 'course', 'training'

    if ( is_product_category( array('events', $y) ) ) {
    global $product;
    wp_list_categories( 
      array(
          'show_option_all' => 0,
          'taxonomy' => 'product_cat',
          'style' => 'none',
          'separator' => '',
          'hide_empty' => 0,
          'child_of' => $term_id,
      ));

  }

}
  • 写回答

1条回答 默认 最新

  • drema2014 2019-08-10 05:23
    关注

    The function get_term_by() doesn't work with "id", but with "term_id" instead (or "slug" or "name").
    The function get_term_children() gives an array of Terms IDs.
    The conditional function is_product_category() accepts an array of term names, slugs or Ids, as it's based on is_tax() function.

    You are making things more complicated than they should be and you don't need a foreach loop.

    To target a specific product category and its related children:

    1) On Archive pages:

    $term_slug = 'events';
    $taxonomy  = 'product_cat';
    
    $term_id   = get_term_by( 'slug', $term_slug, $taxonomy )->term_id; // Get the term ID
    $child_ids = get_term_children( $term_id, $taxonomy ); // Get the children terms IDs
    $terms_ids = array_merge( $child_ids, array($term_id) ); // an array of all term IDs (main term Id and it's children)
    
    if ( is_product_category( $terms_ids ) ) {
        // Do something
    }
    

    So in you code:

    add_action( 'woocommerce_before_shop_loop', 'eventsTerms', 18 );
    function eventsTerms() {
        $term_slug = 'events';
        $taxonomy  = 'product_cat';
    
        $term_id   = get_term_by( 'slug', $term_slug, $taxonomy )->term_id; // Get the term ID
        $child_ids = get_term_children( $term_id, $taxonomy ); // Get the children terms IDs
        $terms_ids = array_merge( $child_ids, array($term_id) ); // an array of all term IDs (main term Id and it's children)
    
        if ( is_product_category( $terms_ids ) ) {
            global $product;
    
            wp_list_categories( array(
                'show_option_all' => 'show all',
                'taxonomy'        => 'product_cat',
                'style'           => 'none',
                'separator'       => '',
                'hide_empty'      => 0,
                'child_of'        => $term_id,
            ) );
        }
    }
    

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


    2) On product pages, cart items or order items (instead of archive pages):

    You will use conditional function has_term(), that accepts also an array of term names, slugs or Ids (The third argument (optional) is the post ID or the product ID for WooCommerce products):

    $term_slug = 'events';
    $taxonomy  = 'product_cat';
    
    $term_id   = get_term_by( 'slug', $term_slug, $taxonomy )->term_id; // Get the term ID
    $child_ids = get_term_children( $term_id, $taxonomy ); // Get the children terms IDs
    $terms_ids = array_merge( $child_ids, [$term_id] ); // an array of all term IDs (main term Id and it's children)
    
    if ( has_term( $terms_ids, $taxonomy, $product_id ) ) {
        // Do something
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图