Advertisement
lorro

WooCommerce - Add gallery image to products in a category

Dec 28th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.55 KB | None | 0 0
  1. <?php
  2.   // add a gallery image to all products in a specific category
  3.   // code goes in functions.php for your child theme
  4.   // in this example, 12 is the category id and 2931 is the media library image id.
  5.   add_filter('woocommerce_product_gallery_attachment_ids', 'add_gallery_image', 10, 2);
  6.   function add_gallery_image($ids, $product) {
  7.     $terms = get_the_terms( $product->post->ID, 'product_cat' );
  8.     foreach ($terms as $term) {
  9.       if ($term->term_id == 12) {
  10.         $ids[] = '2931';
  11.         break;
  12.       }
  13.     }
  14.     return $ids;
  15.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement