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

达米安

我正在尝试在商店内容之前添加自定义内容,但仅在使用此子类别查看特定类别的产品时才添加。

鞋子类别为:主要类别- “活动”,子类别- “研讨会”“课程”“培训”

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()args中使用所以我正在尝试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,
      ));

  }

}
LoicTheAztec

该功能get_term_by()不工作"id""term_id"不是 (或"slug""name"
该函数get_term_children()提供了一个术语ID数组
由于条件函数is_product_category()基于is_tax()函数,因此它接受术语名称,子句或ID的数组

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

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

1)在存档页面上:

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

因此,在您的代码中:

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

代码进入您的活动子主题(active active theme)的functions.php文件中。经过测试和工作。


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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

WooCommerce:CSS-定位特定产品类别

从显示类别为Woocommerce的子类别的产品类别中删除边栏

从 WooCommerce 产品类别小部件中删除父类别和子类别

在Woocommerce档案中获取当前产品类别的子类别

列出Woocommerce中给定产品类别的子类别

从Woocommerce中的子产品类别ID获取父产品类别ID

从WooCommerce中的“相关产品”中排除产品类别

在Woocommerce中检查产品类别的产品

获取Woocommerce中特定产品类别的特定产品属性值

Woocommerce 特定的横幅产品类别和子项

在Woocommerce结帐中为产品类别中的特定虚拟产品启用送货地址

在Woocommerce产品类别页面中更改特定的产品按钮文本

用Woocommerce 3中特定产品类别的产品替换加售商品

隐藏Woocommerce中具有特定产品类别的产品

显示Woocommerce中特定产品类别的产品

从 Woocommerce 的特定产品类别中删除“未找到产品”

取消设置woocommerce中特定产品类别的产品选项卡

在Woocommerce中仅允许购买特定产品类别的一种产品

检查产品是否属于 Woocommerce 中的特定产品类别

列出WooCommerce中产品类别的主要产品子类别

从WooCommerce的订单项中获取产品类别ID

在Woocommerce中根据产品类别更改货币符号

在Woocommerce中基于产品类别隐藏价格

获取WooCommerce中的所有产品类别

更改WooCommerce产品类别标题?

Woocommerce:产品类别小部件

woocommerce获取产品类别ID

Woocommerce的分层产品类别插件

避免在Woocommerce中更新特定产品类别的购物车价格