woocommerce_catalog_ordering()
Output the product sorting options.
Hooks from the function
Returns
null. Nothing (null).
Usage
woocommerce_catalog_ordering( $attributes );
- $attributes(array|null)
- Block attributes.
Default:null
woocommerce_catalog_ordering() woocommerce catalog ordering code WC 10.5.0
function woocommerce_catalog_ordering( $attributes = null ) {
if ( ! wc_get_loop_prop( 'is_paginated' ) || ! woocommerce_products_will_display() ) {
return;
}
/**
* Filter the default catalog orderby.
*
* @since 1.6.4
*
* @param string $default_orderby The default orderby option.
*/
$show_default_orderby = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', 'menu_order' ) );
if ( isset( $attributes ) && isset( $attributes['useLabel'] ) && $attributes['useLabel'] ) {
/**
* Filters the catalog orderby options.
*
* @since 9.7.0
* @param array $catalog_orderby_options Array of catalog orderby options.
*/
$catalog_orderby_options = apply_filters(
'woocommerce_catalog_orderby',
array(
'menu_order' => __( 'Default', 'woocommerce' ),
'popularity' => __( 'Popularity', 'woocommerce' ),
'rating' => __( 'Average rating', 'woocommerce' ),
'date' => __( 'Latest', 'woocommerce' ),
'price' => __( 'Price: low to high', 'woocommerce' ),
'price-desc' => __( 'Price: high to low', 'woocommerce' ),
)
);
} else {
// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
$catalog_orderby_options = apply_filters(
'woocommerce_catalog_orderby',
array(
'menu_order' => __( 'Default sorting', 'woocommerce' ),
'popularity' => __( 'Sort by popularity', 'woocommerce' ),
'rating' => __( 'Sort by average rating', 'woocommerce' ),
'date' => __( 'Sort by latest', 'woocommerce' ),
'price' => __( 'Sort by price: low to high', 'woocommerce' ),
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' ),
)
);
}
$default_orderby = wc_get_loop_prop( 'is_search' ) ? 'relevance' : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', '' ) );
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$orderby = isset( $_GET['orderby'] ) ? wc_clean( wp_unslash( $_GET['orderby'] ) ) : $default_orderby;
// phpcs:enable WordPress.Security.NonceVerification.Recommended
if ( wc_get_loop_prop( 'is_search' ) ) {
$catalog_orderby_options = array_merge( array( 'relevance' => __( 'Relevance', 'woocommerce' ) ), $catalog_orderby_options );
unset( $catalog_orderby_options['menu_order'] );
}
if ( ! $show_default_orderby ) {
unset( $catalog_orderby_options['menu_order'] );
}
if ( ! wc_review_ratings_enabled() ) {
unset( $catalog_orderby_options['rating'] );
}
if ( is_array( $orderby ) ) {
$orderby = current( array_intersect( $orderby, array_keys( $catalog_orderby_options ) ) );
}
if ( ! array_key_exists( $orderby, $catalog_orderby_options ) ) {
$orderby = current( array_keys( $catalog_orderby_options ) );
}
wc_get_template(
'loop/orderby.php',
array(
'catalog_orderby_options' => $catalog_orderby_options,
'orderby' => $orderby,
'show_default_orderby' => $show_default_orderby,
'use_label' => isset( $attributes['useLabel'] ) ? $attributes['useLabel'] : false,
)
);
}