获取按“菜单顺序”排序的WooCommerce特定产品属性术语

罗伯特·塞萨尔

我想按当前产品的menu_order对get_the_terms进行排序,直到现在我都拥有以下代码:

$colors='';

$terms = get_the_terms( get_the_ID(), 'pa_colors' );
            
foreach ( $terms as $term ) {
        $colors='<pre>'.$term->name.'</pre>';
}
echo $colors;

LoicTheAztec

有两种方法可以获取按菜单顺序排序的产品属性术语名称(对于已定义的产品)

1)。使用wp_get_post_terms()功能 (WordPress方式)

WordPress功能get_the_terms() 不允许更改WP_Term_Query

因此,您将使用wp_get_post_terms()允许WP_Term_Query调整的类似内容

$taxonomy = 'pa_color'; // The taxonomy
$query_args = array(
    'fields'  => 'names',
    'orderby' => 'meta_value_num', 
    'meta_query' => array( array(
        'key' => 'order_' . $taxonomy, 
        'type' => 'NUMERIC'
     ) )
);

$term_names = wp_get_post_terms( get_the_ID(), $taxonomy, $query_args );

if ( ! empty( $term_names ) ) {

    // Output
    echo  '<pre>' . implode( '</pre><pre>', $term_names ) . '</pre>';
}

2)。简单使用WC_Product方法get_attribute() (WooCommerce方式)

$product  = wc_get_product( get_the_ID() ); // The WC_product Object
$taxonomy = 'pa_color'; // The taxonomy

$names_string = $product->get_attribute('color');

if ( ! empty( $names_string ) ) {
    $names_array = explode( ', ', $names_string ); // Converting the string to an array of term names

    // Output
    echo  '<pre>' . implode( '</pre><pre>', $names_array ) . '</pre>';
}

两种方式都可以。

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从所有产品中获取 Woocommerce 中特定产品属性的术语名称数组

来自分类Dev

在WooCommerce中为带有特定产品属性术语的变体制作必填订单注释字段

来自分类Dev

WooCommerce获取具有给定产品ID的产品属性

来自分类Dev

在WooCommerce变动价格上显示特定的选定产品属性

来自分类Dev

如何使用REST API为WooCommerce产品指定产品图片排序顺序?

来自分类Dev

如何在Woocommerce后端中按特定产品属性(在这种情况下为品牌)正确过滤

来自分类Dev

WooCommerce:按分类术语总计获取缺货产品

来自分类Dev

在WordPress / WooCommerce中获取产品属性术语永久链接

来自分类Dev

如何从WooCommerce产品属性分类法中获取术语?

来自分类Dev

显示为WooCommerce产品设置的特定产品属性链接条款

来自分类Dev

要求在WooCommerce中注册特定产品

来自分类Dev

要求在WooCommerce中注册特定产品

来自分类Dev

要求在WooCommerce中注册特定产品

来自分类Dev

要求在WooCommerce中注册特定产品

来自分类Dev

Woocommerce 隐藏特定产品的价格

来自分类Dev

在WooCommerce的末尾对特定产品类别购物车项目进行排序

来自分类Dev

感谢页面重定向以获取WooCommerce订单商品中的特定产品ID

来自分类Dev

在 Woocommerce 产品页面中隐藏特定产品标签的按钮

来自分类Dev

在管理产品常规框中获取WooCommerce产品变体属性术语

来自分类Dev

在WooCommerce WC_Product_Query中按产品属性术语过滤

来自分类Dev

Woocommerce:特定产品的强制性优惠券

来自分类Dev

回显特定产品ID的woocommerce库存数量

来自分类Dev

检查用户是否在WooCommerce中购买了特定产品

来自分类Dev

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

来自分类Dev

Woocommerce REST API排除订单端点中的特定产品

来自分类Dev

WooCommerce自定义缺货文本+特定产品ID

来自分类Dev

避免只针对特定产品的WooCommerce新订单通知

来自分类Dev

WooCommerce根据购买的特定产品的数量收取额外费用

来自分类Dev

Woocommerce:特定产品的强制性优惠券

Related 相关文章

  1. 1

    从所有产品中获取 Woocommerce 中特定产品属性的术语名称数组

  2. 2

    在WooCommerce中为带有特定产品属性术语的变体制作必填订单注释字段

  3. 3

    WooCommerce获取具有给定产品ID的产品属性

  4. 4

    在WooCommerce变动价格上显示特定的选定产品属性

  5. 5

    如何使用REST API为WooCommerce产品指定产品图片排序顺序?

  6. 6

    如何在Woocommerce后端中按特定产品属性(在这种情况下为品牌)正确过滤

  7. 7

    WooCommerce:按分类术语总计获取缺货产品

  8. 8

    在WordPress / WooCommerce中获取产品属性术语永久链接

  9. 9

    如何从WooCommerce产品属性分类法中获取术语?

  10. 10

    显示为WooCommerce产品设置的特定产品属性链接条款

  11. 11

    要求在WooCommerce中注册特定产品

  12. 12

    要求在WooCommerce中注册特定产品

  13. 13

    要求在WooCommerce中注册特定产品

  14. 14

    要求在WooCommerce中注册特定产品

  15. 15

    Woocommerce 隐藏特定产品的价格

  16. 16

    在WooCommerce的末尾对特定产品类别购物车项目进行排序

  17. 17

    感谢页面重定向以获取WooCommerce订单商品中的特定产品ID

  18. 18

    在 Woocommerce 产品页面中隐藏特定产品标签的按钮

  19. 19

    在管理产品常规框中获取WooCommerce产品变体属性术语

  20. 20

    在WooCommerce WC_Product_Query中按产品属性术语过滤

  21. 21

    Woocommerce:特定产品的强制性优惠券

  22. 22

    回显特定产品ID的woocommerce库存数量

  23. 23

    检查用户是否在WooCommerce中购买了特定产品

  24. 24

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

  25. 25

    Woocommerce REST API排除订单端点中的特定产品

  26. 26

    WooCommerce自定义缺货文本+特定产品ID

  27. 27

    避免只针对特定产品的WooCommerce新订单通知

  28. 28

    WooCommerce根据购买的特定产品的数量收取额外费用

  29. 29

    Woocommerce:特定产品的强制性优惠券

热门标签

归档