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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。