定位特定的产品类别,它是WooCommerce中的子类别

时间:2019-08-09 22:45:21

标签: php wordpress woocommerce custom-taxonomy taxonomy-terms

我正在尝试在商店内容之前添加自定义内容,但是仅当查看具有该类别子项的特定类别的产品时。

鞋子类别为:主要类别-'活动',子类别-'研讨会''课程''训练”。

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返回所有子类别名称,因此我想将is_product_category条件标记与数组一起使用,但我的代码仍仅适用于主类别“事件”。

错误在哪里?

编辑1

好的,我认为implode不能在is_product_category()参数中使用它的原因。所以我尝试像这样json_encode

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 个答案:

答案 0 :(得分:0)

函数get_term_by()不适用于"id"却不适用于"term_id" (或"slug"或{{1 }})
函数get_term_children()提供了一组术语ID
条件函数is_product_category()接受术语名称,子词或ID的数组,因为它基于is_tax()函数。

您使事情变得比原本应该复杂的多,并且不需要foreach循环。

要定位特定的产品类别及其相关子类别:

1)在“存档”页面上:

"name"

所以在您的代码中:

$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
}

代码进入活动的子主题(活动的活动主题)的functions.php文件中。经过测试并可以工作。


2)在产品页面,购物车项目或订单项目上 (而不是存档页面)

您将使用条件函数has_term(),该函数还接受术语名称,段或ID的数组。(第三个参数(可选)是WooCommerce产品的帖子ID或产品ID)

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