• Resolved Shmoo

    (@macpresss)


    Don’t know if this is a bug or anything else but for some reason I can’t get the product post class filter not to work.

    function shmoo_wc_product_post_class( $classes, $class = '', $post_id = '' ) {
    
    	if ( ! $post_id || 'product' !== get_post_type( $post_id ) ) {
    		return $classes;
    	}
    
    	$product = wc_get_product( $post_id );
    
    	if ( $product ) {
    		if ( $product->is_on_sale() ) {
    			$classes[] = 'sale';
    		}
    		if ( $product->is_featured() ) {
    			$classes[] = 'featured';
    		}
    	}
    	if ( false !== ( $key = array_search( 'hentry', $classes ) ) ) {
    		unset( $classes[ $key ] );
    	}
    	return $classes;
    }
    add_filter( 'post_class', 'shmoo_wc_product_post_class', 22, 4 );

    When I add this it keeps showing the category, tags and all other class names in the products list.
    This is on a MultiSite install, I believe it has worked before on a single install like this.

    https://wordpress.org/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi Shmoo,

    It will work on multisite install as well as on single site install. Here, In above code, you have added ‘classes’ but not removed, already added classes. Here, this filter will run after woocommerce filter and it will already contain classes added by, ‘wc_product_post_class()’ function of WooCommerce.

    You can use below code,

    add_action('init','wdm_change_class');
    
        function wdm_change_class()
        {
           remove_filter( 'post_class', 'wc_product_post_class', 20, 3 );
           add_filter( 'post_class', 'shmoo_wc_product_post_class', 22, 3);
        }

    Thread Starter Shmoo

    (@macpresss)

    Thanks for your help, but for some reason the product_category_names still end up in the post_class,

    I like to clean up the class element and only show the class names that really matter. Like Featured, sale and product or post_id related.

    If classes still appears, it means ‘remove_filter’ doesn’t worked perfectly.

    Where have you added above code?

    There may be possibility theme or other plugin adding some post classes or you may also need to try out some other hook other than ‘init’, however above code work with WooCommerce & ‘Twenty Thirteen’ theme.

    Thread Starter Shmoo

    (@macpresss)

    Just in the functions.php

    I always use the Underscores.me starters theme by Automatitic to create from scratch.

    I have to freeze this issue tho, I can’t spend too much time figuring this filter out because it’s not really a bug but just one of the many fancy features in WordPress I don’t like. I don’t want 25 class names ( tags, cats , post types, id’s, template names and other junk ) in a body tag or wrapper element just because WordPress can do this.
    I want to keep things lean, only add class names when they really mean something or do something based on functionality.

    At some point I’ll figure this out.

    Thanks anyway.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Bug? – product post class filter doesn't work’ is closed to new replies.