0

I noticed in my wordpress/woocommerce setup that every time i add a tag to a product or a blog post. It adds that tag as a class into the listed item (Product/blog)

I also noticed it adds a class in the same place for every category i put these post items into.

How can i prevent wordpress and woocommerce from addings these tag and category names into my html markup as classes? As i do not need them and its creating a mess.

Thanks for any help! I could not locate a similar function anywhere.

2 Answers 2

0

You have to leverage the woocommerce wc_product_post_class filter. The following code should do the trick.


function my_strip_tag_class( $classes ) {

    global $post;
    $product_tags = get_the_terms( $post->ID, 'product_tag' );

    if( $product_tags ) foreach ( $product_tags as $tag ) {

        $tag_index = array_search($tag, $classes);

          if($tag_index !== false){
            unset($classes[$tag_index]);
        }

    }
    return $classes;
}
add_filter( 'wc_product_post_class', 'my_strip_tag_class' );

4
  • Hello and thanks for the answer. When trying to apply your code to my functions.php file i get this error "Your PHP code changes were rolled back due to an error on line 638 of file wp-content/plugins/woocommerce/includes/wc-template-functions.php. Please fix and try saving again. Cannot redeclare wc_get_product_class() (previously declared in wp-content/themes/child-theme/functions.php:839)"
    – Ian Garvey
    Feb 15, 2019 at 11:38
  • Sorry, I made a terrible mistake in my answer, I will edit it Feb 15, 2019 at 11:43
  • Please let me know once you are done editing it.
    – Ian Garvey
    Feb 15, 2019 at 11:54
  • @KYSSE take a look now Feb 15, 2019 at 13:35
0

For WooCommerce 3.6.2+ try the woocommerce_post_class filter:

add_filter('woocommerce_post_class', function($classes, $product) {
    // remove whatever classes you want here...
    return $classes;
}, 10, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.