Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Last active July 12, 2021 14:04
Show Gist options
  • Save jorpdesigns/e892bfb462526520ed6699d3eab8e25d to your computer and use it in GitHub Desktop.
Save jorpdesigns/e892bfb462526520ed6699d3eab8e25d to your computer and use it in GitHub Desktop.
Snippet to check WooCommerce product category, tag, sale status, virtual status, downloadable status & type
<?php
global $product;
$productID = $product->get_id();
// CHECK IF PRODUCT BELONGS TO CATEGORY
if ( has_term( 'term', 'product_cat', $productID ) ) { //term can be category name, id, slug or an array of each to check for
// Do something
}
// CHECK IF PRODUCT BELONGS TO TAG
if ( has_term( 'term', 'product_tag', $productID ) ) { //term can be category name, id, slug or an array of each to check for
// Do something
}
// CHECK IF PRODUCT IS ON SALE
if ( $product->is_on_sale() ) {
// Do something
}
// CHECK IF PRODUCT IS VIRTUAL
if( $product->is_virtual() ){
// Do something
}
// CHECK IF PRODUCT IS DOWNLOADABLE
if( $product->is_downloadable() ){
// Do something
}
/**
* CHECK PRODUCT TYPE
*/
// GET PRODUCT TYPE
$productType = get_the_terms( $productID, 'product_type')[0]->slug;
if ( $product->is_type( 'simple' ) ){
// Do something
} elseif( $product->is_type( 'variable' ) ){
// Do something
} elseif( $product->is_type( 'external' ) ){
// Do something
} elseif( $product->is_type( 'grouped' ) ){
// Do something
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment