针对特定产品类别及其在 WooCommerce 中的子项
Target specific product category and it's children in WooCommerce
我正在尝试在商店内容之前添加自定义内容,但只有在查看特定类别的产品时才会使用此类别的子项。
鞋子类别有:主类别 - 'events',子类别 - 'seminarium','course' 和 '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 所有子类别名称,所以我想使用 is_product_category
带数组的条件标记,但我的代码仍然只适用于主类别 "events"。
错误在哪里?
编辑 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_]*)":/',':', $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,
));
}
}
函数 get_term_by()
不适用于 "id"
、,而是 "term_id"
(或 "slug"
或 "name"
)。
函数 get_term_children()
给出了一个包含 个术语 ID 的数组。
条件函数is_product_category()
accepts an array of term names, slugs or Ids, as it's based on is_tax()
函数.
你让事情变得比它们应该的更复杂,你不需要 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,
) );
}
}
代码进入您的活动子主题(活动活动主题)的 functions.php 文件。 已测试并有效。
2) 在产品页面、购物车项目或订单项目上 (而不是存档页面):
您将使用条件函数 has_term()
,它还接受一组术语名称、slug 或 ID (第三个参数(可选)是 post ID 或WooCommerce 产品的产品 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
}
我正在尝试在商店内容之前添加自定义内容,但只有在查看特定类别的产品时才会使用此类别的子项。
鞋子类别有:主类别 - 'events',子类别 - 'seminarium','course' 和 '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 所有子类别名称,所以我想使用 is_product_category
带数组的条件标记,但我的代码仍然只适用于主类别 "events"。
错误在哪里?
编辑 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_]*)":/',':', $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,
));
}
}
函数 get_term_by()
不适用于 "id"
、,而是 "term_id"
(或 "slug"
或 "name"
)。
函数 get_term_children()
给出了一个包含 个术语 ID 的数组。
条件函数is_product_category()
accepts an array of term names, slugs or Ids, as it's based on is_tax()
函数.
你让事情变得比它们应该的更复杂,你不需要 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,
) );
}
}
代码进入您的活动子主题(活动活动主题)的 functions.php 文件。 已测试并有效。
2) 在产品页面、购物车项目或订单项目上 (而不是存档页面):
您将使用条件函数 has_term()
,它还接受一组术语名称、slug 或 ID (第三个参数(可选)是 post ID 或WooCommerce 产品的产品 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
}