我正在尝试在商店内容之前添加自定义内容,但仅在使用此子类别查看特定类别的产品时才添加。
鞋子类别为:主要类别- “活动”,子类别- “研讨会”,“课程”和“培训”。
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,
));
}
}
该功能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] 删除。
我来说两句