Brian Duncan
Brian Duncan

Reputation: 135

How to apply CSS to all pages except certain product pages in Wordpress/Woocommerce?

I would like to know how to apply this CSS code to every page on my website except for woocommerce products with a category of 'Category1'.

.custom-single-product .cart, span.woocommerce-Price-amount.amount, a.cart-customlocation.et-cart-info {
    display: none !important;
}

I have tried adding a category class to the tag with php using the code below but I have been unsuccessful in having it work for what I want.

// add taxonomy term to body_class
function woo_custom_taxonomy_in_body_class( $classes ){
  if( is_singular( 'product' ) )
  {
    $custom_terms = get_the_terms(0, 'product_cat');
    if ($custom_terms) {
      foreach ($custom_terms as $custom_term) {
        $classes[] = 'product_cat_' . $custom_term->slug;
      }
    }
  }
  return $classes;
}
add_filter( 'body_class', 'woo_custom_taxonomy_in_body_class' );

Basically, the end goal is to have certain products able to be added to cart and show prices and checkout, but to have those options and information hidden on the vast majority of the products and the website. Thanks

Upvotes: 2

Views: 565

Answers (1)

Brian Duncan
Brian Duncan

Reputation: 135

Add body:not(.product_cat_Category1) to each CSS rule. So it should look like this:

body:not(.product_cat_Category1) .custom-single-product .cart, body:not(.product_cat_Category1) span.woocommerce-Price-amount.amount, body:not(.product_cat_Category1) a.cart-customlocation.et-cart-info {
    display: none !important;
} 

Upvotes: 3

Related Questions