从可变产品中设置的属性中获取所有值

Get all the values from the attributes set in a variable product

在 WooCommerce 中,我试图获取彼此独立的变量产品的所有值。

例如:
我有属性大小。 尺寸有三个值:尺寸 S、尺寸 M 和尺寸 L。
如果产品具有所有尺寸选项,我希望在输出中看到该产品三次:

通过以下代码,我可以看到我有哪些变体。此外,不幸的是我没有来。

private function readOptions($product) {
    $variations = $product->get_children();
    foreach ($variations as $variation) {
        $single_variation=new WC_Product_Variation($variation);
        $this->doLog(print_r($single_variation->get_variation_attributes(), true));
    }              
}

有人知道怎么做吗?

谢谢。

我在这里得到了你的函数代码(评论很好):

private function readOptions($product) {

    // Get all attributes & values set in the product (product variations)
    $variation_attributes = $product->get_variation_attributes();

    // iterating through each attribute
    foreach ($variation_attributes as $attribute_slug => $attribute_slug_values){

        // Getting the attribute object (the taxonomy object)
        $attribute_obj = get_taxonomy( $attribute_slug ); // Attribute name: $attribute_obj->label

        // Iterating through each attribute values set in the product
        foreach($attribute_slug_values as $attribute_slug_value){

            // Getting the object of the term value (and the name: $attr_value_obj->name)
            $attr_value_obj = get_term_by( 'slug', $attribute_slug_value, $attribute_slug );

            // Output the names of the product, the attribute and the value
            echo '<p>Product "'.$product->get_title().'" has attribute "'.$attribute_obj->label.'" with value "'.$attr_value_obj->name.'"</p>';
        }
    }          
}

你会得到你所期待的。

Here the output is just an example, but you get for each attribute and values set in the product:
The product + an attribute + a value (for all attribute values set in it).