diff --git a/includes/class-wc-shortcodes.php b/includes/class-wc-shortcodes.php index a76f640cc67..58363f9b5ac 100644 --- a/includes/class-wc-shortcodes.php +++ b/includes/class-wc-shortcodes.php @@ -87,6 +87,7 @@ class WC_Shortcodes { $products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts, $loop_name ) ); $columns = absint( $atts['columns'] ); $woocommerce_loop['columns'] = $columns; + $woocommerce_loop['name'] = $loop_name; ob_start(); diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index efd7a5dc072..695310b49e6 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -106,13 +106,14 @@ if ( ! function_exists( 'woocommerce_reset_loop' ) ) { /** * Reset the loop's index and columns when we're done outputting a product loop. - * * @subpackage Loop */ function woocommerce_reset_loop() { - global $woocommerce_loop; - // Reset loop/columns globals when starting a new loop - $woocommerce_loop['loop'] = $woocommerce_loop['columns'] = ''; + $GLOBALS['woocommerce_loop'] = array( + 'loop' => '', + 'columns' => '', + 'name' => '', + ); } } add_filter( 'loop_end', 'woocommerce_reset_loop' ); @@ -220,6 +221,26 @@ function wc_product_cat_class( $class = '', $category = null ) { echo 'class="' . esc_attr( join( ' ', wc_get_product_cat_class( $class, $category ) ) ) . '"'; } +/** + * Get classname for loops based on $woocommerce_loop global. + * @since 2.6.0 + * @return string + */ +function wc_get_loop_class() { + global $woocommerce_loop; + + $woocommerce_loop['loop'] = ! empty( $woocommerce_loop['loop'] ) ? $woocommerce_loop['loop'] + 1 : 1; + $woocommerce_loop['columns'] = ! empty( $woocommerce_loop['columns'] ) ? $woocommerce_loop['columns'] : apply_filters( 'loop_shop_columns', 4 ); + + if ( 0 === ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 === $woocommerce_loop['columns'] ) { + return 'first'; + } elseif ( 0 === $woocommerce_loop['loop'] % $woocommerce_loop['columns'] ) { + return 'last'; + } else { + return ''; + } +} + /** * Get the classes for the product cat div. * @@ -228,21 +249,11 @@ function wc_product_cat_class( $class = '', $category = null ) { * @param object $category object Optional. */ function wc_get_product_cat_class( $class = '', $category = null ) { - global $woocommerce_loop; - $classes = is_array( $class ) ? $class : array_map( 'trim', explode( ' ', $class ) ); $classes[] = 'product-category'; $classes[] = 'product'; - - if ( 0 === ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 === $woocommerce_loop['columns'] ) { - $classes[] = 'first'; - } - - if ( 0 === $woocommerce_loop['loop'] % $woocommerce_loop['columns'] ) { - $classes[] = 'last'; - } - - $classes = apply_filters( 'product_cat_class', $classes, $class, $category ); + $classes[] = wc_get_loop_class(); + $classes = apply_filters( 'product_cat_class', $classes, $class, $category ); return array_unique( array_filter( $classes ) ); } @@ -264,6 +275,9 @@ function wc_product_post_class( $classes, $class = '', $post_id = '' ) { $product = wc_get_product( $post_id ); if ( $product ) { + $classes[] = wc_get_loop_class(); + $classes[] = $product->stock_status; + if ( $product->is_on_sale() ) { $classes[] = 'sale'; } @@ -299,6 +313,7 @@ function wc_product_post_class( $classes, $class = '', $post_id = '' ) { $classes[] = 'has-children'; } } + $categories = get_the_terms( $product->id, 'product_cat' ); if ( ! empty( $categories ) ) { $slugs = wp_list_pluck( $categories, 'slug' ); @@ -306,7 +321,6 @@ function wc_product_post_class( $classes, $class = '', $post_id = '' ) { $classes[] = 'product-cat-' . $slug; } } - $classes[] = $product->stock_status; } if ( false !== ( $key = array_search( 'hentry', $classes ) ) ) { @@ -479,6 +493,7 @@ if ( ! function_exists( 'woocommerce_product_loop_start' ) ) { */ function woocommerce_product_loop_start( $echo = true ) { ob_start(); + $GLOBALS['woocommerce_loop']['loop'] = 0; wc_get_template( 'loop/loop-start.php' ); if ( $echo ) echo ob_get_clean(); diff --git a/templates/cart/cross-sells.php b/templates/cart/cross-sells.php index 1efb4855920..8cbce8ed95b 100644 --- a/templates/cart/cross-sells.php +++ b/templates/cart/cross-sells.php @@ -17,16 +17,14 @@ */ if ( ! defined( 'ABSPATH' ) ) { - exit; // Exit if accessed directly + exit; } global $product, $woocommerce_loop; -$crosssells = WC()->cart->get_cross_sells(); - -if ( 0 === sizeof( $crosssells ) ) return; - -$meta_query = WC()->query->get_meta_query(); +if ( ! $crosssells = WC()->cart->get_cross_sells() ) { + return; +} $args = array( 'post_type' => 'product', @@ -35,11 +33,11 @@ $args = array( 'posts_per_page' => apply_filters( 'woocommerce_cross_sells_total', $posts_per_page ), 'orderby' => $orderby, 'post__in' => $crosssells, - 'meta_query' => $meta_query + 'meta_query' => WC()->query->get_meta_query() ); -$products = new WP_Query( $args ); - +$products = new WP_Query( $args ); +$woocommerce_loop['name'] = 'cross-sells'; $woocommerce_loop['columns'] = apply_filters( 'woocommerce_cross_sells_columns', $columns ); if ( $products->have_posts() ) : ?> diff --git a/templates/content-product.php b/templates/content-product.php index 4e315f0c5bf..17da20cb771 100644 --- a/templates/content-product.php +++ b/templates/content-product.php @@ -20,37 +20,14 @@ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } -global $product, $woocommerce_loop; - -// Store loop count we're currently on -if ( empty( $woocommerce_loop['loop'] ) ) { - $woocommerce_loop['loop'] = 0; -} - -// Store column count for displaying the grid -if ( empty( $woocommerce_loop['columns'] ) ) { - $woocommerce_loop['columns'] = apply_filters( 'loop_shop_columns', 4 ); -} +global $product; // Ensure visibility -if ( ! $product || ! $product->is_visible() ) { +if ( empty( $product ) || ! $product->is_visible() ) { return; } - -// Increase loop count -$woocommerce_loop['loop']++; - -// Extra post classes -$classes = array(); -if ( 0 === ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 === $woocommerce_loop['columns'] ) { - $classes[] = 'first'; -} -if ( 0 === $woocommerce_loop['loop'] % $woocommerce_loop['columns'] ) { - $classes[] = 'last'; -} ?> -