Give loops names, and set class outside of template files
Closes #10028
This commit is contained in:
parent
f750d71409
commit
a778c0b234
|
@ -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();
|
||||
|
||||
|
|
|
@ -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,20 +249,10 @@ 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[] = 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();
|
||||
|
|
|
@ -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 );
|
||||
|
||||
$woocommerce_loop['name'] = 'cross-sells';
|
||||
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_cross_sells_columns', $columns );
|
||||
|
||||
if ( $products->have_posts() ) : ?>
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
?>
|
||||
<li <?php post_class( $classes ); ?>>
|
||||
|
||||
<li <?php post_class(); ?>>
|
||||
<?php
|
||||
/**
|
||||
* woocommerce_before_shop_loop_item hook.
|
||||
|
@ -90,5 +67,4 @@ if ( 0 === $woocommerce_loop['loop'] % $woocommerce_loop['columns'] ) {
|
|||
*/
|
||||
do_action( 'woocommerce_after_shop_loop_item' );
|
||||
?>
|
||||
|
||||
</li>
|
||||
|
|
|
@ -19,21 +19,6 @@
|
|||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
global $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 );
|
||||
}
|
||||
|
||||
// Increase loop count.
|
||||
$woocommerce_loop['loop']++;
|
||||
?>
|
||||
<li <?php wc_product_cat_class( '', $category ); ?>>
|
||||
<?php
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
exit;
|
||||
}
|
||||
|
||||
global $product, $woocommerce_loop;
|
||||
|
@ -26,9 +26,9 @@ if ( empty( $product ) || ! $product->exists() ) {
|
|||
return;
|
||||
}
|
||||
|
||||
$related = $product->get_related( $posts_per_page );
|
||||
|
||||
if ( sizeof( $related ) === 0 ) return;
|
||||
if ( ! $related = $product->get_related( $posts_per_page ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$args = apply_filters( 'woocommerce_related_products_args', array(
|
||||
'post_type' => 'product',
|
||||
|
@ -41,8 +41,8 @@ $args = apply_filters( 'woocommerce_related_products_args', array(
|
|||
) );
|
||||
|
||||
$products = new WP_Query( $args );
|
||||
|
||||
$woocommerce_loop['columns'] = $columns;
|
||||
$woocommerce_loop['name'] = 'related';
|
||||
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
|
||||
|
||||
if ( $products->have_posts() ) : ?>
|
||||
|
||||
|
|
|
@ -17,19 +17,15 @@
|
|||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
exit;
|
||||
}
|
||||
|
||||
global $product, $woocommerce_loop;
|
||||
|
||||
$upsells = $product->get_upsells();
|
||||
|
||||
if ( sizeof( $upsells ) === 0 ) {
|
||||
if ( ! $upsells = $product->get_upsells() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$meta_query = WC()->query->get_meta_query();
|
||||
|
||||
$args = array(
|
||||
'post_type' => 'product',
|
||||
'ignore_sticky_posts' => 1,
|
||||
|
@ -38,16 +34,16 @@ $args = array(
|
|||
'orderby' => $orderby,
|
||||
'post__in' => $upsells,
|
||||
'post__not_in' => array( $product->id ),
|
||||
'meta_query' => $meta_query
|
||||
'meta_query' => WC()->query->get_meta_query()
|
||||
);
|
||||
|
||||
$products = new WP_Query( $args );
|
||||
|
||||
$woocommerce_loop['columns'] = $columns;
|
||||
$woocommerce_loop['name'] = 'up-sells';
|
||||
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_up_sells_columns', $columns );
|
||||
|
||||
if ( $products->have_posts() ) : ?>
|
||||
|
||||
<div class="upsells products">
|
||||
<div class="up-sells upsells products">
|
||||
|
||||
<h2><?php _e( 'You may also like…', 'woocommerce' ) ?></h2>
|
||||
|
||||
|
|
Loading…
Reference in New Issue