wc_get_product_class() │ WC 3.4.0 Retrieves the classes for the post div as an array.
This method was modified from WordPress's get_post_class() to allow the removal of taxonomies (for performance reasons). Previously wc_product_post_class was hooked into post_class. @since 3.6.0
Return
Array
.
Usage
wc_get_product_class( $class, $product );
$class(string|array)
One or more classes to add to the class list.
Default: ''
$product(int|WP_Post |WC_Product )
Product ID or product object.
Default: null
Changelog
wc_get_product_class() wc get product class code
WC 9.2.3
function wc_get_product_class( $class = '', $product = null ) {
if ( is_null( $product ) && ! empty( $GLOBALS['product'] ) ) {
// Product was null so pull from global.
$product = $GLOBALS['product'];
}
if ( $product && ! is_a( $product, 'WC_Product' ) ) {
// Make sure we have a valid product, or set to false.
$product = wc_get_product( $product );
}
if ( $class ) {
if ( ! is_array( $class ) ) {
$class = preg_split( '#\s+#', $class );
}
} else {
$class = array();
}
$post_classes = array_map( 'esc_attr', $class );
if ( ! $product ) {
return $post_classes;
}
// Run through the post_class hook so 3rd parties using this previously can still append classes.
// Note, to change classes you will need to use the newer woocommerce_post_class filter.
// @internal This removes the wc_product_post_class filter so classes are not duplicated.
$filtered = has_filter( 'post_class', 'wc_product_post_class' );
if ( $filtered ) {
remove_filter( 'post_class', 'wc_product_post_class', 20 );
}
$post_classes = apply_filters( 'post_class', $post_classes, $class, $product->get_id() );
if ( $filtered ) {
add_filter( 'post_class', 'wc_product_post_class', 20, 3 );
}
$classes = array_merge(
$post_classes,
array(
'product',
'type-product',
'post-' . $product->get_id(),
'status-' . $product->get_status(),
wc_get_loop_class(),
$product->get_stock_status(),
),
wc_get_product_taxonomy_class( $product->get_category_ids(), 'product_cat' ),
wc_get_product_taxonomy_class( $product->get_tag_ids(), 'product_tag' )
);
if ( $product->get_image_id() ) {
$classes[] = 'has-post-thumbnail';
}
if ( $product->get_post_password() ) {
$classes[] = post_password_required( $product->get_id() ) ? 'post-password-required' : 'post-password-protected';
}
if ( $product->is_on_sale() ) {
$classes[] = 'sale';
}
if ( $product->is_featured() ) {
$classes[] = 'featured';
}
if ( $product->is_downloadable() ) {
$classes[] = 'downloadable';
}
if ( $product->is_virtual() ) {
$classes[] = 'virtual';
}
if ( $product->is_sold_individually() ) {
$classes[] = 'sold-individually';
}
if ( $product->is_taxable() ) {
$classes[] = 'taxable';
}
if ( $product->is_shipping_taxable() ) {
$classes[] = 'shipping-taxable';
}
if ( $product->is_purchasable() ) {
$classes[] = 'purchasable';
}
if ( $product->get_type() ) {
$classes[] = 'product-type-' . $product->get_type();
}
if ( $product->is_type( 'variable' ) && $product->get_default_attributes() ) {
$classes[] = 'has-default-attributes';
}
// Include attributes and any extra taxonomies only if enabled via the hook - this is a performance issue.
if ( apply_filters( 'woocommerce_get_product_class_include_taxonomies', false ) ) {
$taxonomies = get_taxonomies( array( 'public' => true ) );
$type = 'variation' === $product->get_type() ? 'product_variation' : 'product';
foreach ( (array) $taxonomies as $taxonomy ) {
if ( is_object_in_taxonomy( $type, $taxonomy ) && ! in_array( $taxonomy, array( 'product_cat', 'product_tag' ), true ) ) {
$classes = array_merge( $classes, wc_get_product_taxonomy_class( (array) get_the_terms( $product->get_id(), $taxonomy ), $taxonomy ) );
}
}
}
/**
* WooCommerce Post Class filter.
*
* @since 3.6.2
* @param array $classes Array of CSS classes.
* @param WC_Product $product Product object.
*/
$classes = apply_filters( 'woocommerce_post_class', $classes, $product );
return array_map( 'esc_attr', array_unique( array_filter( $classes ) ) );
}