WooCommerce-在订单电子邮件中显示特定类别父类别的子类别



因此,我有一个奇怪的请求,要求在WooCommerce中确认订单后发送的订单电子邮件中显示产品被分配到的子类别,但我只想显示特定类别父类别的类别。

你看,我在WooCommerce有两个主要类别,一个是品牌,另一个是类别。我只想在"品牌"类别下显示分配给特定产品的子类别。在我的案例中,品牌类别(父类别(的ID为15。

我目前测试并确认有效的代码片段是这个

function modfuel_woocommerce_before_order_add_cat($name, $item){
$product_id = $item['product_id'];
$_product = wc_get_product( $product_id );
$htmlStr = "";
$cats = "";
$terms = get_the_terms( $product_id, 'product_cat' );
$count = 0;
foreach ( $terms as $term) {
$count++;
if($count > 1){
$cats .= $term->name;
}
else{
$cats .= $term->name . ',';
}
}
$cats = rtrim($cats,',');
$htmlStr .= $_product->get_title();
$htmlStr .= "<p>Category: " . $cats . "</p>";
return $htmlStr;
}
add_filter('woocommerce_order_item_name','modfuel_woocommerce_before_order_add_cat', 10, 2);

这里的人知道我可以在上面的代码中添加什么来获得我需要的东西吗?

谢谢!

如果我现在理解了您的代码,那么您现在已经拥有$terms中的所有类别,并且希望跳过所有不是brand子项的术语。

您可以简单地跳过没有此父项的术语。你的代码会看起来像这样:

function modfuel_woocommerce_before_order_add_cat($name, $item){
$product_id = $item['product_id'];
$_product = wc_get_product( $product_id );
$htmlStr = "";
$cats = "";
$terms = get_the_terms( $product_id, 'product_cat' );
$count = 0;
foreach ( $terms as $term) {
if ($term->parent != 15) continue;
$count++;
if($count > 1){
$cats .= $term->name;
}
else{
$cats .= $term->name . ',';
}
}
$cats = rtrim($cats,',');
$htmlStr .= $_product->get_title();
$htmlStr .= "<p>Category: " . $cats . "</p>";
return $htmlStr;
}
add_filter('woocommerce_order_item_name','modfuel_woocommerce_before_order_add_cat', 10, 2);

我添加了if ($term->parent != 15) continue;代码,如果它不是Brand(ID:15(的直接子项,则跳过该术语。

最新更新