英文:
Add parent name to term list in dropdown (eg. coupon category list)
问题
Sure, here's the translated content:
我们有一家WooCommerce服装商店,想要创建排除特定类别的优惠券。我们的类别结构如下(简化版):
- 男装
- 衬衫
- 牛仔裤
- 特价
- 女装
- 衬衫
- 牛仔裤
- 特价
然而,当我们想要在[WooCommerce › 营销 › 优惠券 › 使用限制]的“排除类别”下拉选项中选择[男装 › 特价]时,列表被扁平化了:
- 牛仔裤
- 男装
- 特价
- 特价
- 衬衫
- 衬衫
- 女装
现在不清楚哪些类别实际上是我们要排除的。请注意,这只是一个非常简化的结构,在实际环境中情况更糟糕。
我原本希望有一个apply_filter
,但是在代码中似乎没有get_terms()
的筛选器,请参见GitHub上的源代码 »(第233行)
$categories = get_terms('product_cat', 'orderby=name&hide_empty=0');
由于这没有被筛选,我看不到如何将父类别添加为所需的结果:
- 男装
- 男装 › 牛仔裤
- 男装 › 衬衫
- 男装 › 特价
- 女装
- 女装 › 牛仔裤
- 女装 › 衬衫
- 女装 › 特价
有没有办法在没有代码筛选器的情况下获取该列表的任何想法?
例如,这是我们用于第三方插件的代码,该插件也使用WooCommerce类别术语,但实际上在get_terms
上有一个筛选器,而原生的WooCommerce优惠券代码却没有:
if (!function_exists('yith_wcbep_add_parent_category_name')) {
function yith_wcbep_add_parent_category_name($terms, $args) {
$term_name = '';
$taxonomy = isset($args['taxonomy']) ? $args['taxonomy'] : '';
foreach ($terms as $term_id => $term_name) {
$term = get_term_by('id', $term_id, $taxonomy);
$term_name = isset($term->name) ? $term->name : '';
if (isset($term->parent) && $term->parent > 0) {
$parent = get_term_by('id', $term->parent, $term->taxonomy);
$term_name = $parent->name . ' > ' . $term_name;
$terms[$term_id] = $term_name;
}
}
return $terms;
}
add_filter('yith_plugin_fw_json_search_found_terms', 'yith_wcbep_add_parent_category_name', 10, 2);
}
英文:
We have a WooCommerce clothing store and want to create coupons that have certain categories excluded. Our category structure is as follows (simplified):
– Men
– – Shirts
– – Jeans
– – Sale
– Women
– – Shirts
– – Jeans
– – Sale
However when we want to select [Men » Sale] from the Exclude categories dropdown picker in [WooCommerce » Marketing » Coupons » Usage Restriction] the list is flattened:
– Jeans
– Men
– Sale
– Sale
– Shirts
– Shirts
– Women
Now it is not clear which categories are actually the one we want to exclude. Have in min, this is a very simplified structure, in the live environment this is even worse.
I hoped for an apply_filter
but the get_terms()
apparently does have none in code, see source code on github here » (line #233)
$categories = get_terms('product_cat', 'orderby=name&hide_empty=0');
Since this is not filtered, I don’t see a way to add a parent category to have this as the desired outcome:
- Men
- Men » Jeans
- Men » Shirts
- Men » Sale
- Women
- Women » Jeans
- Women » Shirts
- Women » Sale
Any idea how to get that list now that there is no filter in the code?
For example, this is what we use for a third party plugin that also uses the WooCommerce category terms, but that actually does have a filter on the get_terms and the native WooCommerce code on coupons does not...
Example:
if (!function_exists('yith_wcbep_add_parent_category_name')) {
function yith_wcbep_add_parent_category_name($terms, $args) {
$term_name = '';
$taxonomy = isset($args['taxonomy']) ? $args['taxonomy'] : '';
foreach ($terms as $term_id => $term_name) {
$term = get_term_by('id', $term_id, $taxonomy);
$term_name = isset($term->name) ? $term->name : '';
if (isset($term->parent) && $term->parent > 0) {
$parent = get_term_by('id', $term->parent, $term->taxonomy);
$term_name = $parent->name . ' > ' . $term_name;
$terms[$term_id] = $term_name;
}
}
return $terms;
}
add_filter('yith_plugin_fw_json_search_found_terms', 'yith_wcbep_add_parent_category_name', 10, 2);
}
</details>
# 答案1
**得分**: 3
为了解决这个问题,你可以克隆 WooCommerce 优惠券产品类别限制部分,自定义类别输出,例如在类别名称中添加父级名称,然后使用 JavaScript 从默认的 WooCommerce 输出中移除相关的 HTML 输出。这样你会得到以下结果:
[![enter image description here][1]][1]
以下是钩子函数的代码:
```php
// 在术语名称输出之前添加父级术语名称
function prepend_parent_term_name( $term, $sep = ' > ' ) {
if (isset($term->parent) && $term->parent > 0) {
$parent_name = get_term_by('id', $term->parent, $term->taxonomy)->name;
return esc_html( $parent_name ) . $sep . esc_html( $term->name );
} else {
return esc_html( $term->name );
}
}
// 用自定义内容替换 WooCommerce 管理员优惠券产品类别限制部分
add_action( 'woocommerce_coupon_options_usage_restriction', 'action_coupon_options_usage_restriction', 10, 2 );
function action_coupon_options_usage_restriction( $coupon_id, $coupon ) {
echo '<div class="options_group">';
// 类别。
?>
<p class="form-field">
<label for="product_categories2"><?php _e( 'Product categories', 'woocommerce' ); ?></label>
<select id="product_categories2" name="product_categories[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'Any category', 'woocommerce' ); ?>">
<?php
$category_ids = $coupon->get_product_categories( 'edit' );
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );
if ( $categories ) {
foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . wc_selected( $cat->term_id, $category_ids ) . '>' . prepend_parent_term_name( $cat ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Product categories that the coupon will be applied to, or that need to be in the cart in order for the "Fixed cart discount" to be applied.', 'woocommerce' ) ); ?>
</p>
<?php // 排除类别。 ?>
<p class="form-field">
<label for="exclude_product_categories2"><?php _e( 'Exclude categories', 'woocommerce' ); ?></label>
<select id="exclude_product_categories2" name="exclude_product_categories[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'No categories', 'woocommerce' ); ?>">
<?php
$category_ids = $coupon->get_excluded_product_categories( 'edit' );
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );
if ( $categories ) {
foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . wc_selected( $cat->term_id, $category_ids ) . '>' . prepend_parent_term_name( $cat ) . '</option>';
}
}
?>
</select>
<?php echo wc_help_tip( __( 'Product categories that the coupon will not be applied to, or that cannot be in the cart in order for the "Fixed cart discount" to be applied.', 'woocommerce' ) ); ?>
</p>
<?php // 移除默认产品类别限制的 HTML 输出。 ?>
<script>
jQuery(function($){
$('#product_categories').parent().parent().remove();
});
</script>
<?php
echo '</div>';
}
将这段代码添加到你的活动子主题(或活动主题)的 functions.php 文件中,经过测试可行。
英文:
To solve this issue, what you can do is to clone WooCommerce coupon product categories restrictions section, customizing the categories output as you like *(here I add the parent term name to the category name), removing with JavaScript the related HTML output from the default WooCommerce ones.
So you will get the following:
Here is the hooked function code:
// Prepend the parent term name to the term name output
function prepend_parent_term_name( $term, $sep = ' > ' ) {
if (isset($term->parent) && $term->parent > 0) {
$parent_name = get_term_by('id', $term->parent, $term->taxonomy)->name;
return esc_html( $parent_name ) . $sep . esc_html( $term->name );
} else {
return esc_html( $term->name );
}
}
// Replace WooCommerce Admin Coupon product categories restictions section with a custom one
add_action( 'woocommerce_coupon_options_usage_restriction', 'action_coupon_options_usage_restriction', 10, 2 );
function action_coupon_options_usage_restriction( $coupon_id, $coupon ) {
echo '<div class="options_group">';
// Categories.
?>
<p class="form-field">
<label for="product_categories2"><?php _e( 'Product categories', 'woocommerce' ); ?></label>
<select id="product_categories2" name="product_categories[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'Any category', 'woocommerce' ); ?>">
<?php
$category_ids = $coupon->get_product_categories( 'edit' );
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );
if ( $categories ) {
foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . wc_selected( $cat->term_id, $category_ids ) . '>' . prepend_parent_term_name( $cat ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Product categories that the coupon will be applied to, or that need to be in the cart in order for the "Fixed cart discount" to be applied.', 'woocommerce' ) ); ?>
</p>
<?php // Exclude Categories. ?>
<p class="form-field">
<label for="exclude_product_categories2"><?php _e( 'Exclude categories', 'woocommerce' ); ?></label>
<select id="exclude_product_categories2" name="exclude_product_categories[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'No categories', 'woocommerce' ); ?>">
<?php
$category_ids = $coupon->get_excluded_product_categories( 'edit' );
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );
if ( $categories ) {
foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . wc_selected( $cat->term_id, $category_ids ) . '>' . prepend_parent_term_name( $cat ) . '</option>';
}
}
?>
</select>
<?php echo wc_help_tip( __( 'Product categories that the coupon will not be applied to, or that cannot be in the cart in order for the "Fixed cart discount" to be applied.', 'woocommerce' ) ); ?>
</p>
<?php // Removing default product categories restrictions html ?>
<script>
jQuery(function($){
$('#product_categories').parent().parent().remove();
});
</script>
<?php
echo '</div>';
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
答案2
得分: 2
这是一个有趣的功能解决方法,似乎很奇怪Woocommerce在优惠券分类中没有层次结构名称。
所以我进行了一些调查,发现唯一合适的过滤器似乎是get_terms
,经过一些尝试和错误,我得出了以下结果。
if (!function_exists('btAddHierarchyNameToCouponsCat')) {
function btAddHierarchyNameToCouponsCat($terms)
{
if (!is_admin()) {
return $terms;
}
global $pagenow;
if ($pagenow !== 'post-new.php' && $pagenow !== 'post.php') {
return $terms;
}
if (
(
!empty($_GET['post_type'])
&& $_GET['post_type'] === 'shop_coupon'
)
|| (
!empty($_GET['post'])
&& get_post_type($_GET['post']) === 'shop_coupon'
&& !empty($_GET['action'])
&& $_GET['action'] === 'edit'
)
) {
foreach ($terms as &$term) {
$term->name = (function ($term) {
$fullName = array_map(function ($ancestorId) {
$ancestor = get_term_by('term_id', $ancestorId, 'product_cat');
return $ancestor->name;
}, array_reverse(get_ancestors($term->term_id, 'product_cat')));
$fullName[] = $term->name;
return implode(' -> ', $fullName);
})($term);
}
return $terms;
}
return $terms;
}
add_filter('get_terms', 'btAddHierarchyNameToCouponsCat');
}
这是之前的样子。
这是之后的样子。
这个过滤器的一般思想是仅在仪表板上创建或编辑优惠券时才起作用。
然后它更新分类名称以包含所有类别名称的整个层次结构。
这可能不是完美的,但它是一个起点。
英文:
This was an interesting feature to solve, seems kind of strange Woocommerce doesn't have hierarchy names in the coupons categories.
So I did some digging and found that the only appropriate filter seems to be get_terms
, after some trial and error I came up with this.
if (!function_exists('btAddHierarchyNameToCouponsCat')) {
function btAddHierarchyNameToCouponsCat($terms)
{
if (!is_admin()) {
return $terms;
}
global $pagenow;
if ($pagenow !== 'post-new.php' && $pagenow !== 'post.php') {
return $terms;
}
if (
(
!empty($_GET['post_type'])
&& $_GET['post_type'] === 'shop_coupon'
)
|| (
!empty($_GET['post'])
&& get_post_type($_GET['post']) === 'shop_coupon'
&& !empty($_GET['action'])
&& $_GET['action'] === 'edit'
)
) {
foreach ($terms as &$term) {
$term->name = (function ($term) {
$fullName = array_map(function ($ancestorId) {
$ancestor = get_term_by('term_id', $ancestorId, 'product_cat');
return $ancestor->name;
}, array_reverse(get_ancestors($term->term_id, 'product_cat')));
$fullName[] = $term->name;
return implode(' -> ', $fullName);
})($term);
}
return $terms;
}
return $terms;
}
add_filter('get_terms', 'btAddHierarchyNameToCouponsCat');
}
Here is the before.
And here is the after.
The general idea of this filter is to specifically work only when creating or editing a coupon on the dashboard.
Then it updates the category name to contain the whole hierarchy of the categories names.
This might not be perfect but it's a starting point
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论