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; } }
The page I need help with: [log in to see the link]
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Woocommerce change default product images to their category image’ is closed to new replies.