• Hi,
    I would like to use this code i have to display category and subcategory images from a random product image, if there is not an existing uploaded image for the category. The only problem is, when i activate the code, it doubles the placeholders for category and subcategory pages. So on the parent category page i get double the images as i have uploaded category images, and on the subcategories I get the desired random image, and a blank placeholder. Is it possible to hide the category image to make way for the image produced by the plugin?
    I have pasted the code below….
    Thank You,
    Rohan

    
    /**
     * Plugin Name: WooCommerce Category Images Modification
     * Plugin URI: https://blog.ashfame.com/?p=1117
     * Description: Use product image as its category image on category archive pages (To override image for product category, upload one for that category and it will override)
     * Author: Ashfame
     * Version: 0.1.2
     * Author URI: https://ashfame.com/
     */
    
    class WooCommerce_Category_Images_From_Product {
    
    	private $let_category_image_override = true;
    	private $randomize_category_image_from_products = true;
    
    	public function __construct() {
    		// Unhooking core's and hooking our custom thumbnail
    		add_action( 'plugins_loaded', array( $this, 'overrides' ) );
    		add_action( 'woocommerce_before_subcategory_title', array( $this, 'add_product_image_as_woocommerce_subcategory_thumbnail' ) );
    
    		// Support link in plugins listing
    		add_filter( 'plugin_action_links', array( $this, 'support_plugin_action_link' ), 10, 2 );
    	}
    
    	public function overrides() {
    		remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10 );
    	}
    
    	public function add_product_image_as_woocommerce_subcategory_thumbnail( $category ) {
    
    		if ( $this->let_category_image_override ) {
    			if ( get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true ) ) {
    				woocommerce_subcategory_thumbnail( $category );
    				return;
    			}
    		}
    
    		$query_args = array(
    			'posts_per_page' => $this->randomize_category_image_from_products ? 10 : 1,
    			'post_status' => 'publish',
    			'post_type' => 'product',
    			'tax_query' => array(
    				array(
    					'taxonomy' => 'product_cat',
    					'field' => 'id',
    					'terms' => $category->term_id
    				)
    			)
    		);
    
    		$products = get_posts( $query_args );
    
    		if ( $products ) {
    			echo get_the_post_thumbnail( $products[ array_rand( $products ) ]->ID, 'shop_thumbnail' );
    		}
    	}
    
    	public function support_plugin_action_link( $links, $file ) {
    		if ( $file == plugin_basename( __FILE__ ) ) {
    			$support_link = '<a href="mailto:[email protected]?subject=' . rawurlencode('Premium Support') . '">Premium Support</a>';
    			array_unshift( $links, $support_link );
    		}
    
    		return $links;
    	}
    }
    
    new WooCommerce_Category_Images_From_Product();

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 25 total)
  • hannah

    (@hannahritner)

    Hi,
    Try adding this to your custom css:

    .product-category.grid_item.product img {
        display: none;
    }

    Does that work for you?

    Hannah

    Thread Starter rospam

    (@rospam)

    Hello,
    Thank you for your help.
    It does hide the placeholders in the theme, but unfortunately now does not show any images for categories or subcategories at all.
    Any other ideas?
    I appreciate your help with this.
    Kind Regards
    Rohan

    hannah

    (@hannahritner)

    I assume you haven’t added the script yet? Because right now I’m only seeing a single image. It’s hard to get accurate css only seeing one image. Are you able to add the script so I can see the double image?

    Hannah

    Thread Starter rospam

    (@rospam)

    Hi,
    Ok will do it now, I’m afraid my site is live though so please could you let me know when you have seen it.
    Thanks
    Rohan

    Thread Starter rospam

    (@rospam)

    Looks like the script may be the default woocommerce image size for the duplicate image, the virtue image is the wide format image.

    hannah

    (@hannahritner)

    Did you remove it already? Are you referring to your shop page?

    Hannah

    Thread Starter rospam

    (@rospam)

    No still on
    Yes shop page there are 2 category images for all categories.
    Thanks

    Thread Starter rospam

    (@rospam)

    Just turned it off, hopefully you saw it.
    Thanks

    hannah

    (@hannahritner)

    I didn’t see it when I checked before. Do you think you could send a screenshot of this?

    Hannah

    Thread Starter rospam

    (@rospam)

    hannah

    (@hannahritner)

    Yeah I never saw that. Hmm, it’s the images added through the script that would need to be removed because the theme images are the ones with the title. The problem is without being able to see it I can’t provide accurate css. I will be on for the next 2 hours if you can make it live again?

    Hannah

    Thread Starter rospam

    (@rospam)

    Hi,
    Thank you Hannah, I have made the script live ??
    Kind Regards

    hannah

    (@hannahritner)

    I’m not seeing the double images. What browser are you using? Have you cleared your cache?

    Hannah

    Thread Starter rospam

    (@rospam)

    Face-palm of the millennium! Thank you so much ?? Cleared my cache and no copies……but…

    However, I have just turned on subcategories and they have the extra placeholder!

    https://www.dropbox.com/s/zp887n0hvpxvtt5/Subcats%20image.JPG?dl=0

    If you could sanity check me on that it would be appreciated. Hopefully not another ‘Sixth Sense’ scenario with me saying ‘I see placeholders!’ and you saying ‘where, where…?’

    Thank You,
    Rohan

    Thread Starter rospam

    (@rospam)

    And the script produced images look a bit small, but i’m not sure if that would be because the other placeholder is there…?

    Thanks

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Can I hide product category image placeholders?’ is closed to new replies.