Last active
September 20, 2021 13:12
-
-
Save jorpdesigns/9e736786eb5fa8bca5aa0f089ceaa610 to your computer and use it in GitHub Desktop.
Snippet to add body class on WooCommerce single product page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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