Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // For a given category, set all product featured images to the product's category image
- // relevant products must have one category only
- // version 1.2
- // usage: make a page with this shortcode: [set_product_images cat_name="my_cat_name"]
- // use "all" for my_cat_name to set all product featured images to their respective category images
- // load the page in a browser to run the script
- // code goes in your child theme's functions.php
- // For live shops, backup the database before using
- // THERE IS NO UNDO!
- add_shortcode('set_product_images', 'set_product_images');
- function set_product_images($attributes) {
- $defaults = array('cat_name' => 'None');
- $parameters = shortcode_atts( $defaults, $attributes);
- // get the cat_name
- $cat_name = $parameters['cat_name']; // number of products to show
- switch ($cat_name) {
- case 'none':
- print '<p>Error: Category name not set in shortcode attributes.</p>'.PHP_EOL;
- break;
- case 'all':
- // get all product categories
- $product_cats = get_terms('product_cat', 'fields => "names"');
- foreach($product_cats as $product_cat){
- $this_cat_name = $product_cat->name;
- set_product_images_for_category($this_cat_name);
- }
- break;
- default:
- set_product_images_for_category($cat_name);
- }
- }
- function set_product_images_for_category($cat_name) {
- global $wpdb;
- echo '<p>Category name: '.$cat_name.'</p>'.PHP_EOL;
- // get the term
- $category = get_term_by('name', $cat_name, 'product_cat');
- if ($category === false) {
- print '<p>Error: The category name which was set in the shortcode\'s attributes was not found.</p>'.PHP_EOL;
- } else {
- $category_id = $category->term_id;
- echo '<p>Category ID: '.$category_id.'</p>';
- // get the category image
- $thumbnail_id = get_woocommerce_term_meta( $category_id, 'thumbnail_id', true );
- if (!$thumbnail_id) {
- print '<p>Error: There is no category image set for this category.</p>'.PHP_EOL;
- } else {
- echo '<p>Thumbnail ID: '.$thumbnail_id.'</p>'.PHP_EOL;
- $image_url = wp_get_attachment_url( $thumbnail_id );
- echo '<p>Category image url: '.$image_url.'</p>';
- if ($image_url) {
- echo '<img src="'.$image_url.'" alt="" />'.PHP_EOL;
- }
- // get all the products
- $nr_products = 0;
- $nr_products_changed = 0;
- $arguments = array('post_type' => 'product', 'posts_per_page' => -1);
- $the_query = new WP_Query( $arguments );
- // the loop
- if ( $the_query->have_posts() ) {
- while ( $the_query->have_posts() ) {
- $the_query->the_post();
- $nr_products++;
- // get the product categories
- $terms = get_the_terms( get_the_ID(), 'product_cat' );
- // "false" if the post contains no terms from the given taxonomy
- if ($terms !== false) {
- if(is_wp_error($terms)) {
- echo '<p>'.$return->get_error_message().'</p>';
- } else {
- // maybe 1 or many categories, we are only interested if there is 1 category
- if (count($terms) == 1) {
- foreach ($terms as $term) {
- // is it the category we are looking for?
- if ($term->name == $cat_name) {
- // found!
- echo '<p>Post ID: '.get_the_ID().' Product category name: '.$term->name.'</p>'.PHP_EOL;
- set_post_thumbnail( $post, $thumbnail_id);
- echo '<p>Product featured image set to category image.</p>'.PHP_EOL;
- $nr_products_changed++;
- }
- }
- }
- }
- }
- }
- echo '<p>Number of products found in the database: '.$nr_products.'</p>'.PHP_EOL;
- echo '<p>Number of products whose featured image was changed: '.$nr_products_changed.'</p>'.PHP_EOL;
- } else {
- // no posts found
- }
- // restore original Post Data
- wp_reset_postdata();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement