Merge pull request #18042 from woocommerce/update/loops-and-globals

Loops and globals
This commit is contained in:
Claudiu Lodromanean 2017-12-06 11:15:38 -08:00 committed by GitHub
commit 83e1043c7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 239 additions and 235 deletions

View File

@ -151,8 +151,6 @@ class WC_Shortcodes {
* @return string * @return string
*/ */
public static function product_categories( $atts ) { public static function product_categories( $atts ) {
global $woocommerce_loop;
if ( isset( $atts['number'] ) ) { if ( isset( $atts['number'] ) ) {
$atts['limit'] = $atts['number']; $atts['limit'] = $atts['number'];
} }
@ -202,7 +200,8 @@ class WC_Shortcodes {
} }
$columns = absint( $atts['columns'] ); $columns = absint( $atts['columns'] );
$woocommerce_loop['columns'] = $columns;
wc_set_loop_prop( 'columns', $columns );
ob_start(); ob_start();

View File

@ -507,12 +507,14 @@ class WC_Shortcode_Products {
$query = new WP_Query( $this->query_args ); $query = new WP_Query( $this->query_args );
} }
$paginated = ! $query->get( 'no_found_rows' );
$results = (object) array( $results = (object) array(
'ids' => wp_parse_id_list( $query->posts ), 'ids' => wp_parse_id_list( $query->posts ),
'total' => (int) $query->found_posts, 'total' => $paginated ? (int) $query->found_posts : count( $query->posts ),
'total_pages' => (int) $query->max_num_pages, 'total_pages' => $paginated ? (int) $query->max_num_pages : 1,
'per_page' => (int) $query->get( 'posts_per_page' ), 'per_page' => (int) $query->get( 'posts_per_page' ),
'current_page' => (int) max( 1, $query->get( 'paged', 1 ) ), 'current_page' => $paginated ? (int) max( 1, $query->get( 'paged', 1 ) ) : 1,
); );
if ( $cache ) { if ( $cache ) {
@ -533,13 +535,8 @@ class WC_Shortcode_Products {
* @return string * @return string
*/ */
protected function product_loop() { protected function product_loop() {
global $woocommerce_loop;
$columns = absint( $this->attributes['columns'] ); $columns = absint( $this->attributes['columns'] );
$classes = $this->get_wrapper_classes( $columns ); $classes = $this->get_wrapper_classes( $columns );
$woocommerce_loop['columns'] = $columns;
$woocommerce_loop['name'] = $this->type;
$woocommerce_loop['shortcode'] = true;
$products = $this->get_query_results(); $products = $this->get_query_results();
ob_start(); ob_start();
@ -549,21 +546,27 @@ class WC_Shortcode_Products {
update_meta_cache( 'post', $products->ids ); update_meta_cache( 'post', $products->ids );
update_object_term_cache( $products->ids, 'product' ); update_object_term_cache( $products->ids, 'product' );
// Setup the loop.
wc_setup_loop( array(
'columns' => $columns,
'name' => $this->type,
'is_shortcode' => true,
'is_search' => false,
'is_paginated' => wc_string_to_bool( $this->attributes['paginate'] ),
'total' => $products->total,
'total_pages' => $products->total_pages,
'per_page' => $products->per_page,
'current_page' => $products->current_page,
) );
$original_post = $GLOBALS['post']; $original_post = $GLOBALS['post'];
do_action( "woocommerce_shortcode_before_{$this->type}_loop", $this->attributes ); do_action( "woocommerce_shortcode_before_{$this->type}_loop", $this->attributes );
do_action( 'woocommerce_before_shop_loop' );
if ( true === wc_string_to_bool( $this->attributes['paginate'] ) ) {
woocommerce_result_count( array(
'total' => $products->total,
'per_page' => $products->per_page,
'current' => $products->current_page,
) );
woocommerce_catalog_ordering();
}
woocommerce_product_loop_start(); woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
foreach ( $products->ids as $product_id ) { foreach ( $products->ids as $product_id ) {
$GLOBALS['post'] = get_post( $product_id ); // WPCS: override ok. $GLOBALS['post'] = get_post( $product_id ); // WPCS: override ok.
setup_postdata( $GLOBALS['post'] ); setup_postdata( $GLOBALS['post'] );
@ -577,28 +580,20 @@ class WC_Shortcode_Products {
// Restore product visibility. // Restore product visibility.
remove_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) ); remove_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) );
} }
}
$GLOBALS['post'] = $original_post; // WPCS: override ok. $GLOBALS['post'] = $original_post; // WPCS: override ok.
woocommerce_product_loop_end(); woocommerce_product_loop_end();
if ( true === wc_string_to_bool( $this->attributes['paginate'] ) ) { do_action( 'woocommerce_after_shop_loop' );
woocommerce_pagination( array(
'total' => $products->total_pages,
'current' => $products->current_page,
'base' => esc_url_raw( add_query_arg( 'product-page', '%#%', false ) ),
'format' => '?product-page=%#%',
) );
}
do_action( "woocommerce_shortcode_after_{$this->type}_loop", $this->attributes ); do_action( "woocommerce_shortcode_after_{$this->type}_loop", $this->attributes );
wp_reset_postdata(); wp_reset_postdata();
wc_reset_loop();
} else { } else {
do_action( "woocommerce_shortcode_{$this->type}_loop_no_results", $this->attributes ); do_action( "woocommerce_shortcode_{$this->type}_loop_no_results", $this->attributes );
} }
woocommerce_reset_loop();
return '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . ob_get_clean() . '</div>'; return '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . ob_get_clean() . '</div>';
} }

View File

@ -276,21 +276,6 @@ if ( ! function_exists( 'is_filtered' ) ) {
} }
} }
if ( ! function_exists( 'is_shortcode_loop' ) ) {
/**
* Returns true when called within a loop generated by a products shortcode.
*
* @since 3.3.0
* @return bool
*/
function is_shortcode_loop() {
global $woocommerce_loop;
return apply_filters( 'woocommerce_is_shortcode_loop', ! empty( $woocommerce_loop['shortcode'] ) );
}
}
if ( ! function_exists( 'taxonomy_is_product_attribute' ) ) { if ( ! function_exists( 'taxonomy_is_product_attribute' ) ) {
/** /**

View File

@ -148,54 +148,81 @@ function wc_setup_product_data( $post ) {
} }
add_action( 'the_post', 'wc_setup_product_data' ); add_action( 'the_post', 'wc_setup_product_data' );
if ( ! function_exists( 'woocommerce_reset_loop' ) ) {
/** /**
* Reset the loop's index and columns when we're done outputting a product loop. * Sets up the woocommerce_loop global from the passed args or from the main query.
*/
function woocommerce_reset_loop() {
$GLOBALS['woocommerce_loop'] = array(
'loop' => '',
'columns' => '',
'name' => '',
);
}
}
add_filter( 'loop_end', 'woocommerce_reset_loop' );
/**
* Products RSS Feed.
* *
* @deprecated 2.6 * @since 3.3.0
* @access public * @param array $args Args to pass into the global.
*/ */
function wc_products_rss_feed() { function wc_setup_loop( $args = array() ) {
// Product RSS. if ( isset( $GLOBALS['woocommerce_loop'] ) ) {
if ( is_post_type_archive( 'product' ) || is_singular( 'product' ) ) { return; // If the loop has already been setup, bail.
$feed = get_post_type_archive_feed_link( 'product' );
echo '<link rel="alternate" type="application/rss+xml" title="' . esc_attr__( 'New products', 'woocommerce' ) . '" href="' . esc_url( $feed ) . '" />';
} elseif ( is_tax( 'product_cat' ) ) {
$term = get_term_by( 'slug', esc_attr( get_query_var( 'product_cat' ) ), 'product_cat' );
if ( $term ) {
$feed = add_query_arg( 'product_cat', $term->slug, get_post_type_archive_feed_link( 'product' ) );
/* translators: %s: category name */
echo '<link rel="alternate" type="application/rss+xml" title="' . esc_attr( sprintf( __( 'New products added to %s', 'woocommerce' ), $term->name ) ) . '" href="' . esc_url( $feed ) . '" />';
} }
} elseif ( is_tax( 'product_tag' ) ) {
$term = get_term_by( 'slug', esc_attr( get_query_var( 'product_tag' ) ), 'product_tag' ); $default_args = array(
'loop' => 0,
'columns' => wc_get_default_products_per_row(),
'name' => '',
'is_shortcode' => false,
'is_paginated' => true,
'is_search' => false,
'is_filtered' => false,
'total' => 0,
'total_pages' => 0,
'per_page' => 0,
'current_page' => 1,
);
if ( $term ) { // If this is a main WC query, use global args as defaults.
$feed = add_query_arg( 'product_tag', $term->slug, get_post_type_archive_feed_link( 'product' ) ); if ( $GLOBALS['wp_query']->get( 'wc_query' ) ) {
/* translators: %s: tag name */ $default_args = array_merge( $default_args, array(
echo '<link rel="alternate" type="application/rss+xml" title="' . sprintf( esc_attr__( 'New products tagged %s', 'woocommerce' ), rawurlencode( $term->name ) ) . '" href="' . esc_url( $feed ) . '" />'; 'is_search' => $GLOBALS['wp_query']->is_search(),
'is_filtered' => is_filtered(),
'total' => $GLOBALS['wp_query']->found_posts,
'total_pages' => $GLOBALS['wp_query']->max_num_pages,
'per_page' => $GLOBALS['wp_query']->get( 'posts_per_page' ),
'current_page' => max( 1, $GLOBALS['wp_query']->get( 'paged', 1 ) ),
) );
} }
$GLOBALS['woocommerce_loop'] = wp_parse_args( $args, $default_args );
} }
add_action( 'woocommerce_before_shop_loop', 'wc_setup_loop' );
/**
* Resets the woocommerce_loop global.
*
* @since 3.3.0
*/
function wc_reset_loop() {
unset( $GLOBALS['woocommerce_loop'] );
}
add_action( 'woocommerce_after_shop_loop', 'woocommerce_reset_loop', 999 );
/**
* Gets a property from the woocommerce_loop global.
*
* @since 3.3.0
* @param string $prop Prop to get.
* @param string $default Default if the prop does not exist.
* @return mixed
*/
function wc_get_loop_prop( $prop, $default = '' ) {
return isset( $GLOBALS['woocommerce_loop'], $GLOBALS['woocommerce_loop'][ $prop ] ) ? $GLOBALS['woocommerce_loop'][ $prop ] : $default;
}
/**
* Sets a property in the woocommerce_loop global.
*
* @since 3.3.0
* @param string $prop Prop to set.
* @param string $value Value to set.
*/
function wc_set_loop_prop( $prop, $value = '' ) {
if ( ! isset( $GLOBALS['woocommerce_loop'] ) ) {
wc_setup_loop();
}
$GLOBALS['woocommerce_loop'][ $prop ] = $value;
} }
/** /**
@ -330,20 +357,21 @@ function wc_get_default_product_rows_per_page() {
} }
/** /**
* Get classname for loops based on $woocommerce_loop global. * Get classname for woocommerce loops.
* *
* @since 2.6.0 * @since 2.6.0
* @return string * @return string
*/ */
function wc_get_loop_class() { function wc_get_loop_class() {
global $woocommerce_loop; $loop_index = wc_get_loop_prop( 'loop', 0 );
$columns = wc_get_loop_prop( 'columns', wc_get_default_products_per_row() );
$woocommerce_loop['loop'] = ! empty( $woocommerce_loop['loop'] ) ? $woocommerce_loop['loop'] + 1 : 1; $loop_index ++;
$woocommerce_loop['columns'] = ! empty( $woocommerce_loop['columns'] ) ? $woocommerce_loop['columns'] : wc_get_default_products_per_row(); wc_set_loop_prop( 'loop', $loop_index );
if ( 0 === ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] || 1 === $woocommerce_loop['columns'] ) { if ( 0 === ( $loop_index - 1 ) % $columns || 1 === $columns ) {
return 'first'; return 'first';
} elseif ( 0 === $woocommerce_loop['loop'] % $woocommerce_loop['columns'] ) { } elseif ( 0 === $loop_index % $columns ) {
return 'last'; return 'last';
} else { } else {
return ''; return '';
@ -513,8 +541,6 @@ if ( ! function_exists( 'woocommerce_content' ) ) {
<?php woocommerce_product_loop_start(); ?> <?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : ?> <?php while ( have_posts() ) : ?>
<?php the_post(); ?> <?php the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?> <?php wc_get_template_part( 'content', 'product' ); ?>
@ -642,14 +668,16 @@ if ( ! function_exists( 'woocommerce_product_loop_start' ) ) {
function woocommerce_product_loop_start( $echo = true ) { function woocommerce_product_loop_start( $echo = true ) {
ob_start(); ob_start();
$GLOBALS['woocommerce_loop']['loop'] = 0; wc_set_loop_prop( 'loop', 0 );
wc_get_template( 'loop/loop-start.php' ); wc_get_template( 'loop/loop-start.php' );
$loop_start = apply_filters( 'woocommerce_product_loop_start', ob_get_clean() );
if ( $echo ) { if ( $echo ) {
echo ob_get_clean(); // WPCS: XSS ok. echo $loop_start; // WPCS: XSS ok.
} else { } else {
return ob_get_clean(); return $loop_start;
} }
} }
} }
@ -667,10 +695,12 @@ if ( ! function_exists( 'woocommerce_product_loop_end' ) ) {
wc_get_template( 'loop/loop-end.php' ); wc_get_template( 'loop/loop-end.php' );
$loop_end = apply_filters( 'woocommerce_product_loop_end', ob_get_clean() );
if ( $echo ) { if ( $echo ) {
echo ob_get_clean(); // WPCS: XSS ok. echo $loop_end; // WPCS: XSS ok.
} else { } else {
return ob_get_clean(); return $loop_end;
} }
} }
} }
@ -868,27 +898,16 @@ if ( ! function_exists( 'woocommerce_result_count' ) ) {
/** /**
* Output the result count text (Showing x - x of x results). * Output the result count text (Showing x - x of x results).
*
* @param array $args Pass an associative array of parameters. Uses this if passed, otherwise uses global $wp_query.
*/ */
function woocommerce_result_count( $args = array() ) { function woocommerce_result_count() {
if ( ! woocommerce_products_will_display() ) { if ( ! wc_get_loop_prop( 'is_paginated' ) || ! woocommerce_products_will_display() ) {
return; return;
} }
if ( empty( $args ) ) {
$query = $GLOBALS['wp_query'];
$args = array( $args = array(
'total' => $query->found_posts, 'total' => wc_get_loop_prop( 'total' ),
'per_page' => $query->get( 'posts_per_page' ), 'per_page' => wc_get_loop_prop( 'per_page' ),
'current' => max( 1, $query->get( 'paged', 1 ) ), 'current' => wc_get_loop_prop( 'current_page' ),
); );
}
$args = wp_parse_args( $args, array(
'total' => 0,
'per_page' => 0,
'current' => 0,
) );
wc_get_template( 'loop/result-count.php', $args ); wc_get_template( 'loop/result-count.php', $args );
} }
@ -900,10 +919,9 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
* Output the product sorting options. * Output the product sorting options.
*/ */
function woocommerce_catalog_ordering() { function woocommerce_catalog_ordering() {
if ( ! woocommerce_products_will_display() ) { if ( ! wc_get_loop_prop( 'is_paginated' ) || ! woocommerce_products_will_display() ) {
return; return;
} }
$query = $GLOBALS['wp_query'];
$orderby = isset( $_GET['orderby'] ) ? wc_clean( wp_unslash( $_GET['orderby'] ) ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); $orderby = isset( $_GET['orderby'] ) ? wc_clean( wp_unslash( $_GET['orderby'] ) ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$show_default_orderby = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); $show_default_orderby = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array( $catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array(
@ -915,7 +933,7 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' ), 'price-desc' => __( 'Sort by price: high to low', 'woocommerce' ),
) ); ) );
if ( ! is_shortcode_loop() && $GLOBALS['wp_query']->is_search() ) { if ( wc_get_loop_prop( 'is_search' ) ) {
$catalog_orderby_options = array_merge( array( 'relevance' => __( 'Relevance', 'woocommerce' ) ), $catalog_orderby_options ); $catalog_orderby_options = array_merge( array( 'relevance' => __( 'Relevance', 'woocommerce' ) ), $catalog_orderby_options );
unset( $catalog_orderby_options['menu_order'] ); unset( $catalog_orderby_options['menu_order'] );
if ( 'menu_order' === $orderby ) { if ( 'menu_order' === $orderby ) {
@ -942,25 +960,23 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
if ( ! function_exists( 'woocommerce_pagination' ) ) { if ( ! function_exists( 'woocommerce_pagination' ) ) {
/** /**
* Output the pagination. * Output the pagination. */
* function woocommerce_pagination() {
* @param array $args Pass an associative array of parameters. Uses this if passed, otherwise uses global $wp_query. if ( ! wc_get_loop_prop( 'is_paginated' ) || ! woocommerce_products_will_display() ) {
*/ return;
function woocommerce_pagination( $args = array() ) {
if ( empty( $args ) ) {
$query = $GLOBALS['wp_query'];
$args = array(
'total' => $query->max_num_pages,
'current' => max( 1, $query->get( 'paged', 1 ) ),
);
} }
$args = array(
'total' => wc_get_loop_prop( 'total_pages' ),
'current' => wc_get_loop_prop( 'current_page' ),
);
$args = wp_parse_args( $args, array( if ( wc_get_loop_prop( 'is_shortcode' ) ) {
'total' => 1, $args['base'] = esc_url_raw( add_query_arg( 'product-page', '%#%', false ) );
'current' => 1, $args['format'] = '?product-page = %#%';
'base' => esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ), } else {
'format' => '', $args['base'] = esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) );
) ); $args['format'] = '';
}
wc_get_template( 'loop/pagination.php', $args ); wc_get_template( 'loop/pagination.php', $args );
} }
@ -1211,18 +1227,6 @@ if ( ! function_exists( 'woocommerce_product_additional_information_tab' ) ) {
wc_get_template( 'single-product/tabs/additional-information.php' ); wc_get_template( 'single-product/tabs/additional-information.php' );
} }
} }
if ( ! function_exists( 'woocommerce_product_reviews_tab' ) ) {
/**
* Output the reviews tab content.
*
* @deprecated 2.4.0 Unused.
*/
function woocommerce_product_reviews_tab() {
wc_deprecated_function( 'woocommerce_product_reviews_tab', '2.4' );
}
}
if ( ! function_exists( 'woocommerce_default_product_tabs' ) ) { if ( ! function_exists( 'woocommerce_default_product_tabs' ) ) {
/** /**
@ -1397,7 +1401,7 @@ if ( ! function_exists( 'woocommerce_related_products' ) ) {
* @param array $args Provided arguments. * @param array $args Provided arguments.
*/ */
function woocommerce_related_products( $args = array() ) { function woocommerce_related_products( $args = array() ) {
global $product, $woocommerce_loop; global $product;
if ( ! $product ) { if ( ! $product ) {
return; return;
@ -1419,8 +1423,8 @@ if ( ! function_exists( 'woocommerce_related_products' ) ) {
$args['related_products'] = wc_products_array_orderby( $args['related_products'], $args['orderby'], $args['order'] ); $args['related_products'] = wc_products_array_orderby( $args['related_products'], $args['orderby'], $args['order'] );
// Set global loop values. // Set global loop values.
$woocommerce_loop['name'] = 'related'; wc_set_loop_prop( 'name', 'related' );
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $args['columns'] ); wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_related_products_columns', $args['columns'] ) );
wc_get_template( 'single-product/related.php', $args ); wc_get_template( 'single-product/related.php', $args );
} }
@ -1437,7 +1441,7 @@ if ( ! function_exists( 'woocommerce_upsell_display' ) ) {
* @param string $order Sort direction. * @param string $order Sort direction.
*/ */
function woocommerce_upsell_display( $limit = '-1', $columns = 4, $orderby = 'rand', $order = 'desc' ) { function woocommerce_upsell_display( $limit = '-1', $columns = 4, $orderby = 'rand', $order = 'desc' ) {
global $product, $woocommerce_loop; global $product;
if ( ! $product ) { if ( ! $product ) {
return; return;
@ -1449,8 +1453,9 @@ if ( ! function_exists( 'woocommerce_upsell_display' ) ) {
'orderby' => $orderby, 'orderby' => $orderby,
'columns' => $columns, 'columns' => $columns,
) ); ) );
$woocommerce_loop['name'] = 'up-sells'; wc_set_loop_prop( 'name', 'up-sells' );
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_upsells_columns', isset( $args['columns'] ) ? $args['columns'] : $columns ); wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_upsells_columns', isset( $args['columns'] ) ? $args['columns'] : $columns ) );
$orderby = apply_filters( 'woocommerce_upsells_orderby', isset( $args['orderby'] ) ? $args['orderby'] : $orderby ); $orderby = apply_filters( 'woocommerce_upsells_orderby', isset( $args['orderby'] ) ? $args['orderby'] : $orderby );
$limit = apply_filters( 'woocommerce_upsells_total', isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : $limit ); $limit = apply_filters( 'woocommerce_upsells_total', isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : $limit );
@ -1505,15 +1510,13 @@ if ( ! function_exists( 'woocommerce_cross_sell_display' ) ) {
* @param string $order (default: 'desc'). * @param string $order (default: 'desc').
*/ */
function woocommerce_cross_sell_display( $limit = 2, $columns = 2, $orderby = 'rand', $order = 'desc' ) { function woocommerce_cross_sell_display( $limit = 2, $columns = 2, $orderby = 'rand', $order = 'desc' ) {
global $woocommerce_loop;
if ( is_checkout() ) { if ( is_checkout() ) {
return; return;
} }
// Get visible cross sells then sort them at random. // Get visible cross sells then sort them at random.
$cross_sells = array_filter( array_map( 'wc_get_product', WC()->cart->get_cross_sells() ), 'wc_products_array_filter_visible' ); $cross_sells = array_filter( array_map( 'wc_get_product', WC()->cart->get_cross_sells() ), 'wc_products_array_filter_visible' );
$woocommerce_loop['name'] = 'cross-sells'; wc_set_loop_prop( 'name', 'cross-sells' );
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_cross_sells_columns', $columns ); wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_cross_sells_columns', $columns ) );
// Handle orderby and limit results. // Handle orderby and limit results.
$orderby = apply_filters( 'woocommerce_cross_sells_orderby', $orderby ); $orderby = apply_filters( 'woocommerce_cross_sells_orderby', $orderby );
@ -1710,12 +1713,29 @@ if ( ! function_exists( 'woocommerce_products_will_display' ) ) {
* @return bool * @return bool
*/ */
function woocommerce_products_will_display() { function woocommerce_products_will_display() {
if ( is_search() || is_filtered() || is_paged() || is_shortcode_loop() ) { return 0 < wc_get_loop_prop( 'total', 0 );
return true; }
} }
if ( ! function_exists( 'woocommerce_maybe_show_product_subcategories' ) ) {
/**
* Maybe display categories before, or instead of, a product loop.
*
* @since 3.3.0
* @param string $loop_html HTML.
* @return string
*/
function woocommerce_maybe_show_product_subcategories( $loop_html ) {
// Don't show when filtering, searching or when on page > 1.
if ( 1 < wc_get_loop_prop( 'current_page' ) || wc_get_loop_prop( 'is_search' ) || wc_get_loop_prop( 'is_filtered' ) ) {
return $loop_html;
}
$parent_id = 0;
$display_type = ''; $display_type = '';
// Check categories are enabled and see what level to query.
if ( is_shop() ) { if ( is_shop() ) {
$display_type = get_option( 'woocommerce_shop_page_display', '' ); $display_type = get_option( 'woocommerce_shop_page_display', '' );
} elseif ( is_product_category() ) { } elseif ( is_product_category() ) {
@ -1724,17 +1744,20 @@ if ( ! function_exists( 'woocommerce_products_will_display' ) ) {
$display_type = '' === $display_type ? get_option( 'woocommerce_category_archive_display', '' ) : $display_type; $display_type = '' === $display_type ? get_option( 'woocommerce_category_archive_display', '' ) : $display_type;
} }
// If displaying categories, append to the loop.
if ( '' !== $display_type ) {
ob_start();
woocommerce_product_subcategories( array(
'parent_id' => $parent_id,
) );
$loop_html .= ob_get_clean();
if ( 'subcategories' === $display_type ) { if ( 'subcategories' === $display_type ) {
if ( is_product_category() ) { wc_set_loop_prop( 'total', 0 );
$term = get_queried_object();
if ( $term && ! count( get_term_children( $term->term_id, $term->taxonomy ) ) ) {
return true;
} }
} }
return false;
}
return true; return $loop_html;
} }
} }
@ -1751,33 +1774,11 @@ if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
'before' => '', 'before' => '',
'after' => '', 'after' => '',
'parent_id' => 0, 'parent_id' => 0,
'display_type' => '',
) ); ) );
// Don't show when filtering, searching or when on page > 1.
if ( is_search() || is_filtered() || is_paged() ) {
return;
}
$parent_id = $args['parent_id'];
$display_type = $args['display_type'];
// Check categories are enabled and see what level to query.
if ( is_shop() ) {
$display_type = get_option( 'woocommerce_shop_page_display', '' );
} elseif ( is_product_category() ) {
$parent_id = get_queried_object_id();
$display_type = get_woocommerce_term_meta( $parent_id, 'display_type', true );
$display_type = '' === $display_type ? get_option( 'woocommerce_category_archive_display', '' ) : $display_type;
}
if ( '' === $display_type ) {
return false;
}
// NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( https://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work. // NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( https://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work.
$product_categories = get_categories( apply_filters( 'woocommerce_product_subcategories_args', array( $product_categories = get_categories( apply_filters( 'woocommerce_product_subcategories_args', array(
'parent' => $parent_id, 'parent' => $args['parent_id'],
'menu_order' => 'ASC', 'menu_order' => 'ASC',
'hide_empty' => 0, 'hide_empty' => 0,
'hierarchical' => 1, 'hierarchical' => 1,
@ -1789,7 +1790,10 @@ if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
$product_categories = wp_list_filter( $product_categories, array( 'count' => 0 ), 'NOT' ); $product_categories = wp_list_filter( $product_categories, array( 'count' => 0 ), 'NOT' );
} }
if ( $product_categories ) { if ( ! $product_categories ) {
return false;
}
echo wp_kses_post( $args['before'] ); echo wp_kses_post( $args['before'] );
foreach ( $product_categories as $category ) { foreach ( $product_categories as $category ) {
@ -1800,19 +1804,8 @@ if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
echo wp_kses_post( $args['after'] ); echo wp_kses_post( $args['after'] );
// If we are hiding products disable the loop and pagination.
if ( 'subcategories' === $display_type ) {
global $wp_query;
$wp_query->post_count = 0;
$wp_query->max_num_pages = 0;
}
return true; return true;
} }
return false;
}
} }
if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) { if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
@ -2704,3 +2697,35 @@ add_action( 'wp_head', 'wc_page_noindex' );
function wc_get_theme_slug_for_templates() { function wc_get_theme_slug_for_templates() {
return apply_filters( 'woocommerce_theme_slug_for_templates', get_option( 'template' ) ); return apply_filters( 'woocommerce_theme_slug_for_templates', get_option( 'template' ) );
} }
/**
* Products RSS Feed.
*
* @deprecated 2.6
*/
function wc_products_rss_feed() {
wc_deprecated_function( 'wc_products_rss_feed', '2.6' );
}
if ( ! function_exists( 'woocommerce_reset_loop' ) ) {
/**
* Reset the loop's index and columns when we're done outputting a product loop.
*
* @deprecated 3.3
*/
function woocommerce_reset_loop() {
wc_reset_loop();
}
}
if ( ! function_exists( 'woocommerce_product_reviews_tab' ) ) {
/**
* Output the reviews tab content.
*
* @deprecated 2.4.0 Unused.
*/
function woocommerce_product_reviews_tab() {
wc_deprecated_function( 'woocommerce_product_reviews_tab', '2.4' );
}
}

View File

@ -66,6 +66,11 @@ add_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
add_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 ); add_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 );
add_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 ); add_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 );
/**
* Product loop start.
*/
add_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' );
/** /**
* Products Loop. * Products Loop.
* *

View File

@ -13,7 +13,7 @@
* @see https://docs.woocommerce.com/document/template-structure/ * @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes * @author WooThemes
* @package WooCommerce/Templates * @package WooCommerce/Templates
* @version 2.0.0 * @version 3.3.0
*/ */
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
@ -61,7 +61,6 @@ if ( have_posts() ) :
do_action( 'woocommerce_before_shop_loop' ); do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start(); woocommerce_product_loop_start();
woocommerce_product_subcategories();
while ( have_posts() ) : while ( have_posts() ) :
the_post(); the_post();

View File

@ -15,9 +15,5 @@
* @package WooCommerce/Templates * @package WooCommerce/Templates
* @version 3.3.0 * @version 3.3.0
*/ */
global $woocommerce_loop;
$columns = ! empty( $woocommerce_loop['columns'] ) ? $woocommerce_loop['columns'] : wc_get_default_products_per_row();
?> ?>
<ul class="products columns-<?php echo esc_attr( $columns ); ?>"> <ul class="products columns-<?php echo esc_attr( wc_get_loop_prop( 'columns' ) ); ?>">