tag obtener get_the_tags get_the codex actual php wordpress woocommerce products variations

php - get_the_tags - Obtener los atributos de las variaciones del producto valores ID y nombre del término



tags wordpress codex (1)

En WooCommerce, estoy usando $product->get_variation_attributes() para obtener los atributos de variaciones para un producto. Esta función devuelve una matriz con los nombres sin las ID.

Me gusta esto:

[pa_color-shirt] => Array ( [0] => red [7] => grey [14] => yellow ) [pa_color-sweater] => Array ( [0] => red [1] => green [2] => blue [3] => grey [4] => yellow [5] => pink [6] => dark-blue )

Para la tienda AJAX que estoy creando, también necesito las identificaciones de las variaciones. De modo que puedo agregar los Id y los nombres a los cuadros de selección (Like woocommerce).
Busqué por varios días pero no pude encontrar una solución.

Creé este código:

if($product->has_child()) { $attributes = $product->get_attributes(); $variations = $product->get_available_variations(); $variationsArray = array(); foreach ($attributes as $attr => $attr_deets) { $variationArray = array(); $attribute_label = wc_attribute_label($attr); $variationArray["attribute_label"] = $attribute_label; if (isset($attributes[$attr]) || isset($attributes[''pa_'' . $attr])) { $attribute = isset($attributes[$attr]) ? $attributes[$attr] : $attributes[''pa_'' . $attr]; if ($attribute[''is_taxonomy''] && $attribute[''is_visible'']) { $variationArray["attribute_name"] = $attribute[''name'']; $variationIds = array(); $variationNames = array(); $variationPrices = array(); foreach ($variations as $variation) { if (!empty($variation[''attributes''][''attribute_'' . $attribute[''name'']])) { array_push($variationIds, $variation[''variation_id'']); $taxonomy = $attribute[''name'']; $meta = get_post_meta($variation[''variation_id''], ''attribute_''.$taxonomy, true); $term = get_term_by(''slug'', $meta, $taxonomy); $variation_name = $term->name; array_push($variationNames, $variation_name); array_push($variationPrices, $variation[''display_regular_price'']); } } $variationArray["variation_prices"] = $variationPrices; $variationArray["variations"] = array_combine($variationIds, $variationNames); } } array_push($variationsArray, $variationArray); } } $product_variations = $variationsArray;

Este código devuelve https://hastebin.com/ecebewumoz.php

El código funciona pero devuelve nombres duplicados e id.

Mi pregunta es, ¿alguien sabe cómo podría lograr lo mismo que get_variation_attributes() pero con los ID?

Gracias.


Actualización para WooCommerce versiones 3+

foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ){ // To get the attribute label (in WooCommerce 3+) $taxonomy_label = wc_attribute_label( $taxonomy, $product ); // Setting some data in an array $variations_attributes_and_values[$taxonomy] = array(''label'' => $taxonomy_label); foreach($terms_slug as $term){ // Getting the term object from the slug $term_obj = get_term_by(''slug'', $term, $taxonomy); $term_id = $term_obj->term_id; // The ID <== <== <== <== <== <== HERE $term_name = $term_obj->name; // The Name $term_slug = $term_obj->slug; // The Slug // $term_description = $term_obj->description; // The Description // Setting the terms ID and values in the array $variations_attributes_and_values[$taxonomy][''terms''][$term_id] = array( ''name'' => $term_name, ''slug'' => $term_slug ); } }

Debajo de WooCommerce versión 3

No veo ningún ID de variaciones duplicadas en su matriz de datos sin formato ... Su pregunta no es muy clara, por lo que es difícil adivinar cuáles son los Ids que falta. Entonces corro el riesgo de responder y supongo que los ID faltantes son los términos Ids de los valores de los atributos ...

Para obtener estos ID de términos, uso la función de Wordpress get_term_by() , de esta manera:

foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ){ // To get the taxonomy object $taxonomy_obj = get_taxonomy( $taxonomy ); $taxonomy_name = $taxonomy_obj->name; // Name (we already got it) $taxonomy_label = $taxonomy_obj->label; // Label // Setting some data in an array $variations_attributes_and_values[$taxonomy] = array(''label'' => $taxonomy_obj->label); foreach($terms_slug as $term){ // Getting the term object from the slug $term_obj = get_term_by(''slug'', $term, $taxonomy); $term_id = $term_obj->term_id; // The ID <== <== <== <== <== <== HERE $term_name = $term_obj->name; // The Name $term_slug = $term_obj->slug; // The Slug // $term_description = $term_obj->description; // The Description // Setting the terms ID and values in the array $variations_attributes_and_values[$taxonomy][''terms''][$term_id] = array( ''name'' => $term_name, ''slug'' => $term_slug ); } }

Y con:

echo ''<pre>''; print_r($variations_attributes_and_values);echo ''</pre>'';

Obtendré esta salida, con los ID de términos reales para cada atributo para las variaciones de un producto (he organizado la salida de la matriz para que sea más compacta) :

Array( [pa_color] => Array( [label] => Color [terms] => Array( [8] => Array( [name] => Black [slug] => black'' ) [9] => Array( [name] => Blue [slug] => blue ) [11] => Array( [name] => Green [slug] => green ) ) ) [pa_bulk_quantity] => Array( [label] => Bulk quantity [terms] => Array( [44] => Array( [name] => Pack of 10 dozen [slug] => pack-of-10-dozen ) [45] => Array( [name] => Case of 50 dozens [slug] => case-of-50-dozens ) ) ) )