How to get WooCommerce product variation values How to get WooCommerce product variation values wordpress wordpress

How to get WooCommerce product variation values


You need to use a 2nd foreach loop for the product attributes:

function test_func(){    global $woocommerce, $product, $post;    // test if product is variable    if( $product->is_type( 'variable' ) ){        // Loop through available product variation data        foreach ( $product->get_available_variations() as $key => $variation ) {            // Loop through the product attributes for this variation            foreach ($variation['attributes'] as $attribute => $term_slug ) {                // Get the taxonomy slug                $taxonmomy = str_replace( 'attribute_', '', $attribute );                // Get the attribute label name                $attr_label_name = wc_attribute_label( $taxonmomy );                // Display attribute labe name                $term_name = get_term_by( 'slug', $term_slug, $taxonmomy )->name;                // Testing output                echo '<p>' . $attr_label_name . ': ' . $term_name . '</p>';            }        }    }}