Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Last active February 27, 2025 14:44
Show Gist options
  • Save helgatheviking/d2c06cee8ea925f49148f4404c9f96d3 to your computer and use it in GitHub Desktop.
Save helgatheviking/d2c06cee8ea925f49148f4404c9f96d3 to your computer and use it in GitHub Desktop.
Stripped down, minimalist product classes
<?php
/**
* The template for displaying product content within loops
*
* This template can be overridden by copying it to yourtheme/woocommerce/content-product.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 9.4.0
*/
defined( 'ABSPATH' ) || exit;
global $product;
// Check if the product is a valid WooCommerce product and ensure its visibility before proceeding.
if ( ! is_a( $product, WC_Product::class ) || ! $product->is_visible() ) {
return;
}
?>
<li <?php backcourt_product_class( '', $product ); ?>>
<?php
/**
* Hook: woocommerce_before_shop_loop_item.
*
* @hooked woocommerce_template_loop_product_link_open - 10
*/
do_action( 'woocommerce_before_shop_loop_item' );
/**
* Hook: woocommerce_before_shop_loop_item_title.
*
* @hooked woocommerce_show_product_loop_sale_flash - 10
* @hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
/**
* Hook: woocommerce_shop_loop_item_title.
*
* @hooked woocommerce_template_loop_product_title - 10
*/
do_action( 'woocommerce_shop_loop_item_title' );
/**
* Hook: woocommerce_after_shop_loop_item_title.
*
* @hooked woocommerce_template_loop_rating - 5
* @hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
/**
* Hook: woocommerce_after_shop_loop_item.
*
* @hooked woocommerce_template_loop_product_link_close - 5
* @hooked woocommerce_template_loop_add_to_cart - 10
*/
do_action( 'woocommerce_after_shop_loop_item' );
?>
</li>
<?php
/**
* Stripped down product classes
*
* Drop in replacement for wc_product_class()
*
* @param string|array $class One or more classes to add to the class list.
* @param int|WP_Post|WC_Product $product_id Product ID or product object.
* @return string
*/
function backcourt_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();
}
$classes = array_map( 'esc_attr', $class );
if ( $product ) {
$classes = array_merge(
$classes,
array(
'product',
'type-product',
'post-' . $product->get_id(),
'status-' . $product->get_status(),
wc_get_loop_class(),
$product->get_stock_status(),
),
);
if ( $product->get_type() ) {
$classes[] = 'product-type-' . $product->get_type();
}
}
echo 'class="' . esc_attr( implode( ' ', $classes ) ) . '"';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment