Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Last active September 20, 2021 13:12
Show Gist options
  • Save jorpdesigns/9e736786eb5fa8bca5aa0f089ceaa610 to your computer and use it in GitHub Desktop.
Save jorpdesigns/9e736786eb5fa8bca5aa0f089ceaa610 to your computer and use it in GitHub Desktop.
Snippet to add body class on WooCommerce single product page
<?php
add_filter( 'body_class', 'add_product_type_class' );
function add_product_type_class( $classes ) {
if ( is_product() ){
global $product;
$productID = $product->get_id();
// BASED ON PRODUCT TYPE
$productType = get_the_terms( $productID, 'product_type')[0]->slug;
$classes[] = 'product-type-' . $productType;
// BASED ON PRODUCT CATEGORY
if ( has_term( 'term', 'product_cat', $productID ) ) { // term can be category name, id, slug or an array of each to check for
$classes[] = 'product-category-term';
}
// BASED ON PRODUCT TAG
if ( has_term( 'term', 'product_tag', $productID ) ) { // term can be tag name, id, slug or an array of each to check for
$classes[] = 'product-category-term';
}
}
return $classes;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment