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

我的网络旅馆

我正在尝试从所有产品中获取包含特定产品属性“颜色”的所有自定义术语的数组。

这是我的功能代码:

// custom_terms_taxonomy function

function custom_terms_taxonomy()
{
    $custom_terms = array();
    // array with products
    $products_array = array( 'post_type' => 'product', 'posts_per_page' => -1 );

        foreach ( $products_array as $product_array ) {
        // attribute string
        $terms_array = $product_array->get_attribute( 'pa_color' );
        // array with attributes
        $terms_array = ! empty($terms_array) ? (array) explode(', ', $terms_array) : array();

        foreach ( $terms_array as $term_array ) {
              // check if the value exist aleready in array
              if (!in_array($term_array, $custom_terms)) {
              // add attributes to Custom_array
              array_push($custom_terms,$term_array);
              }
           }
        }

   return $custom_terms;
}
//output
$att_array=custom_terms_taxonomy();
print_r($att_array);

这个脚本不起作用。任何帮助表示赞赏。

LoicTheAztec

您的代码中存在一些错误和缺失的内容……请尝试以下操作:

// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
    $custom_terms = array();
    // array with products
    $products = wc_get_products( array( 'status' => 'publish', 'limit' => -1 ) );

    foreach ( $products as $product ) {
        // attribute string
        $terms_names = $product->get_attribute( 'pa_color' );
        // array with attributes
        $terms_names_array = ! empty($terms_names) ? (array) explode(', ', $terms_names) : array();

        foreach ( $terms_names_array as $terms_name ) {
            // check if the value exist aleready in array
            if ( !in_array( $terms_name, $custom_terms ) ) {
                // add attribute term name in the array
                $custom_terms[] = $terms_name;
            }
        }
    }

   return $custom_terms;
}

// Raw output
print_r(custom_terms_taxonomy());

测试和工作。

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从当前的WooCommerce产品类别中获取所有产品?

来自分类Dev

获取woocommerce类别的所有产品ID

来自分类Dev

woocommerce所有产品尺寸的列表

来自分类Dev

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

来自分类Dev

在 Woocommerce 中,如何查找某个产品是交叉销售的所有产品?

来自分类Dev

隐藏WooCommerce目录中具有特定库存状态的所有产品

来自分类常见问题

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

来自分类Dev

从WooCommerce管理中的所有产品中删除销售价格

来自分类Dev

从WooCommerce管理中的所有产品中删除销售价格

来自分类Dev

如何在 angular 4 中实现 woocommerce api 以列出所有产品

来自分类Dev

在 Woocommerce 存档页面中显示所有产品类型的库存可用性

来自分类Dev

WooCommerce从购物车中删除所有产品,并将当前产品添加到购物车

来自分类Dev

WooCommerce从购物车中删除所有产品,并将当前产品添加到购物车

来自分类Dev

Wordpress Woocommerce更改所有产品为缺货

来自分类Dev

WooCommerce我的帐户-下载:列出所有产品

来自分类Dev

通过WooCommerce按类别显示所有产品

来自分类Dev

SQL查询从woocommerce中选择所有产品

来自分类Dev

如何使用代码模板中的特定标签过滤woocommerce网店中的所有产品?

来自分类Dev

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

来自分类Dev

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

来自分类Dev

显示为WooCommerce产品设置的所有产品属性的简码

来自分类Dev

从表中获取所有产品并对其进行迭代

来自分类Dev

Magento-获取当前类别中的所有产品

来自分类Dev

Woocommerce-获取具有特定属性的类别中的产品

来自分类Dev

在自己的插件中获取所有WooCommerce产品

来自分类Dev

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

来自分类Dev

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

来自分类Dev

WooCommerce - 将所有产品标记为非特色产品

来自分类Dev

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

Related 相关文章

  1. 1

    如何从当前的WooCommerce产品类别中获取所有产品?

  2. 2

    获取woocommerce类别的所有产品ID

  3. 3

    woocommerce所有产品尺寸的列表

  4. 4

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

  5. 5

    在 Woocommerce 中,如何查找某个产品是交叉销售的所有产品?

  6. 6

    隐藏WooCommerce目录中具有特定库存状态的所有产品

  7. 7

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

  8. 8

    从WooCommerce管理中的所有产品中删除销售价格

  9. 9

    从WooCommerce管理中的所有产品中删除销售价格

  10. 10

    如何在 angular 4 中实现 woocommerce api 以列出所有产品

  11. 11

    在 Woocommerce 存档页面中显示所有产品类型的库存可用性

  12. 12

    WooCommerce从购物车中删除所有产品,并将当前产品添加到购物车

  13. 13

    WooCommerce从购物车中删除所有产品,并将当前产品添加到购物车

  14. 14

    Wordpress Woocommerce更改所有产品为缺货

  15. 15

    WooCommerce我的帐户-下载:列出所有产品

  16. 16

    通过WooCommerce按类别显示所有产品

  17. 17

    SQL查询从woocommerce中选择所有产品

  18. 18

    如何使用代码模板中的特定标签过滤woocommerce网店中的所有产品?

  19. 19

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

  20. 20

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

  21. 21

    显示为WooCommerce产品设置的所有产品属性的简码

  22. 22

    从表中获取所有产品并对其进行迭代

  23. 23

    Magento-获取当前类别中的所有产品

  24. 24

    Woocommerce-获取具有特定属性的类别中的产品

  25. 25

    在自己的插件中获取所有WooCommerce产品

  26. 26

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

  27. 27

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

  28. 28

    WooCommerce - 将所有产品标记为非特色产品

  29. 29

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

热门标签

归档