Advertisement
lorro

WooCommerce - Set product image to category image

Nov 19th, 2017
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement