Vous avez un produit variable avec différentes variations de couleur et de la quantité des stocks par variation.
Vous avez donc besoin d'obtenir pour chaque variation: - la quantité de stock de variation: - l'attribut « pa_color » nom du terme de cette variation
En supposant que vous obtenez déjà l'objet WC_Product_Variable
$product
, voici le code :
if ($product->is_type('variable')){
// Get the available variations for the variable product
$available_variations = $product->get_available_variations();
// Initializing variables
$variations_count = count($available_variations);
$loop_count = 0;
// Iterating through each available product variation
foreach($available_variations as $key => $values) {
$loop_count++;
// Get the term color name
$attribute_color = $values['attributes']['attribute_pa_color'];
$wp_term = get_term_by('slug', $attribute_color, 'pa_color');
$term_name = $wp_term->name; // Color name
// Get the variation quantity
$variation_obj = wc_get_product($values['variation_id']);
$stock_qty = $variation_obj->get_stock_quantity(); // Stock qty
// The display
$separator_string = " // ";
$separator = $variations_count < $loop_count ? $separator_string : '';
echo $term_name . ' = ' . $stock_qty . $separator;
}
}
This will exactly output something like (the color name "=" the stock quantity + separator):
Blue = 3 // Red = 4
testé et fonctionne parfaitement dans WooCommerce 3+
Votre question n'est pas très claire, pouvez-vous s'il vous plaît essayer de l'expliquer mieux? – Geoffrey