Advertisement
lorro

WooCommerce - Set product featured images to cat image

Apr 17th, 2015
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.19 KB | None | 0 0
  1. <?php
  2.   // For a given category, set all product featured images to the product's category image
  3.   // relevant products must have one category only
  4.   // version 1.2
  5.   // usage: make a page with this shortcode: [set_product_images cat_name="my_cat_name"]
  6.   // use "all" for my_cat_name to set all product featured images to their respective category images
  7.   // load the page in a browser to run the script
  8.   // code goes in your child theme's functions.php
  9.   // For live shops, backup the database before using
  10.   // THERE IS NO UNDO!
  11.  
  12.   add_shortcode('set_product_images', 'set_product_images');
  13.   function set_product_images($attributes) {
  14.     $defaults = array('cat_name' => 'None');
  15.     $parameters = shortcode_atts( $defaults, $attributes);
  16.     // get the cat_name
  17.     $cat_name = $parameters['cat_name'];  // number of products to show
  18.     switch ($cat_name) {
  19.       case 'none':
  20.         print '<p>Error: Category name not set in shortcode attributes.</p>'.PHP_EOL;
  21.         break;
  22.       case 'all':
  23.         // get all product categories
  24.         $product_cats = get_terms('product_cat', 'fields => "names"');
  25.         foreach($product_cats as $product_cat){
  26.           $this_cat_name = $product_cat->name;
  27.           set_product_images_for_category($this_cat_name);
  28.         }
  29.         break;
  30.       default:
  31.         set_product_images_for_category($cat_name);
  32.     }
  33.   }
  34.  
  35.   function set_product_images_for_category($cat_name) {
  36.     global $wpdb;
  37.     echo '<p>Category name: '.$cat_name.'</p>'.PHP_EOL;
  38.     // get the term
  39.     $category = get_term_by('name', $cat_name, 'product_cat');
  40.     if ($category === false) {
  41.       print '<p>Error: The category name which was set in the shortcode\'s attributes was not found.</p>'.PHP_EOL;
  42.     } else {
  43.       $category_id = $category->term_id;
  44.       echo '<p>Category ID: '.$category_id.'</p>';
  45.       // get the category image
  46.       $thumbnail_id = get_woocommerce_term_meta( $category_id, 'thumbnail_id', true );
  47.       if (!$thumbnail_id) {
  48.         print '<p>Error: There is no category image set for this category.</p>'.PHP_EOL;
  49.       } else {
  50.         echo '<p>Thumbnail ID: '.$thumbnail_id.'</p>'.PHP_EOL;
  51.          $image_url = wp_get_attachment_url( $thumbnail_id );
  52.         echo '<p>Category image url: '.$image_url.'</p>';
  53.         if ($image_url) {
  54.           echo '<img src="'.$image_url.'" alt="" />'.PHP_EOL;
  55.         }
  56.         // get all the products
  57.         $nr_products = 0;
  58.         $nr_products_changed = 0;
  59.         $arguments = array('post_type' => 'product', 'posts_per_page' => -1);
  60.         $the_query = new WP_Query( $arguments );
  61.         // the loop
  62.         if ( $the_query->have_posts() ) {
  63.           while ( $the_query->have_posts() ) {
  64.             $the_query->the_post();
  65.             $nr_products++;
  66.             // get the product categories
  67.             $terms = get_the_terms( get_the_ID(), 'product_cat' );
  68.             // "false" if the post contains no terms from the given taxonomy
  69.             if ($terms !== false) {
  70.               if(is_wp_error($terms)) {
  71.                 echo '<p>'.$return->get_error_message().'</p>';
  72.               } else {
  73.                 // maybe 1 or many categories, we are only interested if there is 1 category
  74.                 if (count($terms) == 1) {
  75.                   foreach ($terms as $term) {
  76.                     // is it the category we are looking for?
  77.                     if ($term->name == $cat_name) {
  78.                       // found!
  79.                       echo '<p>Post ID: '.get_the_ID().' Product category name: '.$term->name.'</p>'.PHP_EOL;
  80.                       set_post_thumbnail( $post, $thumbnail_id);
  81.                       echo '<p>Product featured image set to category image.</p>'.PHP_EOL;
  82.                       $nr_products_changed++;
  83.                     }
  84.                   }
  85.                 }
  86.               }
  87.             }
  88.           }
  89.           echo '<p>Number of products found in the database: '.$nr_products.'</p>'.PHP_EOL;
  90.           echo '<p>Number of products whose featured image was changed: '.$nr_products_changed.'</p>'.PHP_EOL;
  91.         } else {
  92.           // no posts found
  93.         }
  94.         // restore original Post Data
  95.         wp_reset_postdata();
  96.       }
  97.     }
  98.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement