Title: Programmatically adding a class to products
Last modified: August 17, 2018

---

# Programmatically adding a class to products

 *  Resolved [doubleedesign](https://wordpress.org/support/users/doubleedesign/)
 * (@doubleedesign)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-class-to-products/)
 * Trying to work out whether it’s possible to add classes to products using a filter,
   similar to how you do it with `body_class`? Here’s what I have so far, but it’s
   not working:
 *     ```
       function doublee_add_product_rating_class($classes) {
       	// Do stuff to get the classes, e.g.:
       	$classes[] .= 'my-class';
   
       	return $classes;
       }
       add_filter('wc_get_product_class', 'doublee_add_product_rating_class'); 
       ```
   
 * Is something like this possible? If so, what is the correct way to do it?
 * Thanks for your time!

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

 *  [Peter Lawrenson](https://wordpress.org/support/users/lorro/)
 * (@lorro)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-class-to-products/#post-10600840)
 * I think this is the filter you are looking for: ‘product_cat_class’
    [https://docs.woocommerce.com/wc-apidocs/source-function-wc_get_product_cat_class.html#413](https://docs.woocommerce.com/wc-apidocs/source-function-wc_get_product_cat_class.html#413)
 *  Thread Starter [doubleedesign](https://wordpress.org/support/users/doubleedesign/)
 * (@doubleedesign)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-class-to-products/#post-10605109)
 * That’s not the one as I’m trying to add to individual products, however the one
   directly below that is `wc_product_post_class` which is a step closer. I worked
   out that `wc_product_post_class` passes classes to `post_class`, which works 
   for adding my classes to single product templates, but not in the shop loop (
   which is strange because all the classes are otherwise the same in both contexts).
 *  Plugin Support [John Coy a11n](https://wordpress.org/support/users/johndcoy/)
 * (@johndcoy)
 * Automattic Happiness Engineer
 * [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-class-to-products/#post-10633842)
 * Hi [@doubleedesign](https://wordpress.org/support/users/doubleedesign/),
 * Thanks for letting everyone know what works for you. I’ll now resolve this thread.
    -  This reply was modified 7 years, 9 months ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).
 *  Thread Starter [doubleedesign](https://wordpress.org/support/users/doubleedesign/)
 * (@doubleedesign)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-class-to-products/#post-10634189)
 * Actually it’s only half resolved my issue, `wc_product_post_class` applies the
   custom class on the single product template, but not in the loop. I’m still looking
   for advice on the latter.
    -  This reply was modified 7 years, 9 months ago by [doubleedesign](https://wordpress.org/support/users/doubleedesign/).
 *  Plugin Support [John Coy a11n](https://wordpress.org/support/users/johndcoy/)
 * (@johndcoy)
 * Automattic Happiness Engineer
 * [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-class-to-products/#post-10637325)
 * Hi [@doubleedesign](https://wordpress.org/support/users/doubleedesign/),
 * Does any of the functions on this API doc help with that?
 * [https://docs.woocommerce.com/wc-apidocs/package-WooCommerce.Functions.html](https://docs.woocommerce.com/wc-apidocs/package-WooCommerce.Functions.html)

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

The topic ‘Programmatically adding a class to products’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [filter](https://wordpress.org/support/topic-tag/filter/)

 * 4 replies
 * 3 participants
 * Last reply from: [John Coy a11n](https://wordpress.org/support/users/johndcoy/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-adding-a-class-to-products/#post-10637325)
 * Status: resolved