WooCommerce 3 : $product->id $product->get_id()
, . "" WooCommerce ( ) .
:
1) The first method (with the ability to set the size of the thumbnail):
add_action( 'woocommerce_after_shop_loop_item', 'loop_display_variation_attribute_and_thumbnail' );
function loop_display_variation_attribute_and_thumbnail() {
global $product;
if( $product->is_type('variable') ){
foreach ( $product->get_visible_children() as $variation_id ){
$variation = wc_get_product( $variation_id );
$color = $variation->get_attribute('color');
if( ! empty($color) ){
echo $color;
echo $variation->get_image( array(30, 30) );
}
}
}
}
2) Another way:
add_action( 'woocommerce_after_shop_loop_item', 'loop_display_variation_attribute_and_thumbnail' );
function loop_display_variation_attribute_and_thumbnail() {
global $product;
$taxonomy = 'pa_color';
if( $product->is_type('variable') ){
foreach ( $product->get_available_variations() as $variation ){
if( isset($variation['attributes']['attribute_'.$taxonomy]) ){
$term_name = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
echo $term_name;
echo '<img src="' . $variation['image']['thumb_src'] .'">';
}
}
}
}
The code is placed in the function.php file of your active child theme (or active theme). Tested and working.
Related topic: Get URL of Thumbnail Image Variations in WooCommerce
source
share