Get URL of variation image thumbnails in WooCommerce - php

I'm using the following code to grab the image of every variation for a particular product:
$product = new WC_Product_Variable( $product_id );
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
echo "<img src=" . $variation['image']['url'] .">";
}
This returns the full size image.
Can anyone tell me how I would modify this to return the 'thumbnail' URL? (or any other size)
I'm sure it's a fairly simple change but I just can't figure it out.

Use thumb_src instead of url.
$product = new WC_Product_Variable( $product_id );
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
echo "<img src=" . $variation['image']['thumb_src'] .">";
}

If you know the product variation ID, you can do
// find out $variation_id, it's different from product_id
$variation = new WC_Product_Variation( $variation_id );
$image_tag = $variation->get_image();
// The below is the whole image tag including <img> and some classes that will help customize it.
echo $image_tag;
See related docs found at the Woocommerce reference for WC_product_variation class and the WC_Product class. The function get_image() is actually inherited from WC_Product, but you can use it in either class.

Since WooCommerce version 3.0.0 you can get the ID of a variation image as:
$image_id = $variation->get_image_id();
This will return the variation image ID if available, otherwise, the main product image ID will be returned. A variation image could be found in the product back-end as in the image below:
You can then use this image ID to get the URL to your variation or product thumbnail image as:
$image_array = wp_get_attachment_image_src($image_id, 'thumbnail');
$image_src = $image[0];
The code snippet is tested and working.

i have different colors and sizes of clothing.
Is it possible to omit the size and only show the thumbnail of a single color
Right now, it is showing thumbnails for color red in small, medium, and large size. Three duplicated thumbnails.

Related

Woocommerce change default product images to their category image

So, here is the woocommerce setting that I have.
I have a few categores, let’s call them A, B, C, D, E… and so on.
From the backend, I gave category images to each categories.
Now, let’s say I post a product in the category A without selecting any images.
The product just shows the woocommerce default image (the one with the grey background).
Is there a way to use the category images as the default product image when there is no image selected?
I have tried a few codes, this is the last one I have:
add_action( 'init', 'custom_fix_thumbnail');
function custom_fix_thumbnail(){
add_filter('wc_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
function custom_woocommerce_placeholder_img_src( $src ) {
if (is_shop() || is_singular('product') || is_archive() || is_checkout() || is_cart()) {
global $post;
$array = get_the_terms($post->ID, 'product_cat');
reset($array);
$first_key = key($array);
$thumbnail_id = get_woocommerce_term_meta($first_key, 'thumbnail_id', true);
// get the image URL for parent category
$image = wp_get_attachment_url($thumbnail_id);
// print the IMG HTML for parent category
if ($image)
$src = $image;
}
return $src;
}
}
Try changing hook to woocommerce_placeholder_img_src. wc_placeholder_img_src seems to be deprecated.

WooCommerce - Show product category image on single product page template

I seem to be struggling with something in woocommerce.
I have created a single product page template which currently shows the product's featured image at the top of the page in the banner.
However, instead of the product featured image, I would like the banner to display the product category image of the category the product belongs to.
They will only ever belong to one category, so having a fallback isn't that important (I think?!)
To get the product thumbnail I'm using this:
<?php
$image = get_field('product_single_image', 'options');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>">
<?php endif; ?>
So it looks like I could use the same code but replace the product_single_image with get_category_thumbnail or something...
But I think I need to find the category first? I'm not sure where to go from here.
Any help would be massively appreciated.
The best way to get current category ID on single product page and image
global $wp_query;
$terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
foreach ($terms_post as $term_cat) {
$term_cat_id = $term_cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $term_cat_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
echo '<img src="' . $image_url . '">';
}

Woocommerce additionnal variation images - How to display the second image

For the purpose of a product image flipper, I want to display the second image from the variation images for each product.
I'm using the WooCommerce Additional Variation Images plugin and I don't know how to get that image.
I tried this
$product->wp_get_attachment_image_src( $id )
but it returns the following : Call to undefined method WC_Product_Variation::wp_get_attachment_image_src() in...
Anyone who is familiar with this plugin could help me ? If you need more information let me know.
Thanks !
You can get the additional variation images IDs and use these IDs to get the image src like this:
$attachment_ids = get_post_meta( key($values), '_wc_additional_variation_images', true );
$variation_images_src = array();
if ( $attachment_ids ) {
foreach ( $attachment_ids as $attachment_id ) {
$variation_images_src[] = wp_get_attachment_image_src($attachment_id)
}
}
You can get all images for all product variations by this code:
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
echo $variation['image_src'];
}

WooCommerce product list view: shop by subcategory with shared subcategory image in a list view

Sorry, the title may be confused.
So far, all I find about the product list view for woocommerce are displaying all products under a category with their own images. But what I want is that the products are displayed within a category, for instance:
(in the example, "single Door single drawer bases" is the ultimate subcategory e.g Red cabinet->base cabinet(in red)->single door single drawer bases. in this case all the products in this category with different models are displayed in a list view with shared subcategory image.)
Is there any way (code, plugin or any other idea) to achieve this?
Thanks in advance!
To add the category image to the product list you can use this:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_category_image', 100 );
function woocommerce_category_image() {
if ( is_product_category() ){
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<img src="' . $image . '" alt="" />';
}
}
}
To adjust your product list to your needs you can either modify the template content-product.php directly (easy but dirty) or you use the actions hooks in there (recommended but a little bit tricky if you no familiar with this).

Woocommerce: Add the category thumbnail image to my sidebar in product pages

I'm using WooCommerce widgets to add the category list for my products in the sidebar of my website. Currently the categories are listed as text items. I would like to display the thumbnail image for each category (beside the text).
I have used code below, but it shows the category thumbnail in the centre of the page where the products are listed:
add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
function woocommerce_category_image() {
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<img src="' . $image . '" alt="" />';
}
}
I would like this thumbnails in the sidebar beside the text items.
Can anybody help me figure out what I'm missing to achieve this?
Thank you in advance!
You have picked this code snippet at wooThemes: WC Display category image on category archive
You are using woocommerce_archive_description that is displaying this category image on archive-product.php template (line 48).
Sorry but this will not work for a widget especially using this hook.
Now you have 3 options:
Creating your own widget and coding it with php (the hardest).
Using a free plugin made for that: WooCommerce - Category widget (the easiest)
Or in between, picking some code and ideas in the plugin, to make your own…

Categories