从所有产品中获取 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 条评论
登录 后参与评论

相关文章

从WooCommerce中的特定产品属性术语名称获取产品

获取Woocommerce中特定产品属性值的所有产品变体

获取WooCommerce中的所有产品类别

从Woocommerce 3中的特定产品标签更改所有产品价格

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

根据WooCommerce中的一个默认属性值获取所有产品变体

WooCommerce:获取所有产品变型的SKU

使用REST API从Woocommerce获取所有产品

获取woocommerce类别的所有产品ID

获取Woocommerce中特定产品类别的特定产品属性值

在Woocommerce中获取具有特定产品属性的总购买产品数量

woocommerce所有产品尺寸的列表

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

使用SQL在Woocommerce中获取具有特定属性术语和特定类别术语的产品

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

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

在Woocommerce存档页面中显示特定产品属性

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

获取可变产品的每个WooCommerce变体的特定产品属性名称和值

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

获取所有与Woocommerce中与产品类别相关的术语的属性

使特定产品在WooCommerce中互斥

在Woocommerce中处理产品属性的术语名称并对其进行排序

在Woocommerce中显示链接的产品属性术语名称

获取数组中的所有Woocommerce产品ID

在WP_Query中从当前产品类别术语ID获取所有Woocommerce产品

按WooCommerce中查看最多的商品排序和显示所有产品

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

在Woocommerce中几乎所有产品的显示价格上添加后缀