Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

WooCommerce - Set product image to category image

lorro Nov 19th, 2017 (edited) 72 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. <?php
  2.   // WooCommerce - Set product image to category image
  3.   remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
  4.   add_action( 'woocommerce_before_single_product_summary', 'custom_show_product_images', 20 );
  5.   function custom_show_product_images() {
  6.     global $product;
  7.     $product_id = $product->get_id();    
  8.     $cats = get_the_terms( $product_id, 'product_cat' );
  9.     $cat = $cats[0]; // take the first category only
  10.     $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
  11.     $image = wp_get_attachment_url( $thumbnail_id );
  12.     if ( $image ) {
  13.       echo '<img src="'.$image.'" alt="'.$cat->name.'" />';
  14.     }
  15.   } // end function
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top