$product = wc_get_product( $product_id );
if ( $product->is_type( 'variable' ) ) {
$variations = $product->get_available_variations();
if ( empty( $variations ) ) {
return;
}
foreach ( $variations as &$variation ) {
$attribute_values = array();
$attribute_names = array();
foreach ( $variation['attributes'] as $variation_attribute_name => $variation_attribute_value ) {
$taxonomy_name = str_replace( 'attribute_', '', $variation_attribute_name );
if ( ! taxonomy_exists( $taxonomy_name ) ) {
if ( ! isset( $attribute_names[ $variation_attribute_name ] ) ) {
$attribute_names[ $variation_attribute_name ] = $taxonomy_name;
}
$attribute_values[ $variation_attribute_name ] = $variation_attribute_value;
} else {
if ( ! isset( $attribute_names[ $variation_attribute_name ] ) ) {
$taxonomy = get_taxonomy( $taxonomy_name );
$labels = get_taxonomy_labels( $taxonomy );
$attribute_names[ $variation_attribute_name ] = $labels->singular_name;
}
$term = get_term_by( 'slug', $variation_attribute_value, $taxonomy_name );
if ( $term ) {
$attribute_values[ $variation_attribute_name ] = $term->name;
}
}
}
$args['attribute_names'] = $attribute_names;
$args['variations'][] = array(
'id' => $variation['variation_id'],
'sku' => $variation['sku'],
'attributes' => $variation['attributes'],
'attribute_values' => $attribute_values,
);
}
}
$args
такой массив(
[title] => Кроссовки PLAYA CROSSLITE JR
[product_id] => 200
[product_sku] => 204-867-7014
[attribute_names] => Array
(
[attribute_pa_razmer-obuvi-eur] => Размер обуви EUR
)
[variations] => Array
(
[0] => Array
(
[id] => 558
[sku] => 204-867-7014-34
[attributes] => Array
(
[attribute_pa_razmer-obuvi-eur] => sh_eur_34
)
[attribute_values] => Array
(
[attribute_pa_razmer-obuvi-eur] => 34
)
)
[1] => Array
(
[id] => 559
[sku] => 204-867-7014-28
[attributes] => Array
(
[attribute_pa_razmer-obuvi-eur] => sh_eur_28
)
[attribute_values] => Array
(
[attribute_pa_razmer-obuvi-eur] => 28
)
)
...
[12] => Array
(
[id] => 570
[sku] => 204-867-7014-32
[attributes] => Array
(
[attribute_pa_razmer-obuvi-eur] => sh_eur_32
)
[attribute_values] => Array
(
[attribute_pa_razmer-obuvi-eur] => 32
)
)
)
)