Skip to content

Instantly share code, notes, and snippets.

@DeveloperWil
Last active March 8, 2021 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeveloperWil/7ed168632a75e2d012506d6aef94ddcf to your computer and use it in GitHub Desktop.
Save DeveloperWil/7ed168632a75e2d012506d6aef94ddcf to your computer and use it in GitHub Desktop.
WooCommerce: Set default order on shop page to by price, title or date
/**
* Change the default shop page 'catalogue' order of how products are displayed
*
* You can use the following parameters:
* 'menu_order' – by the custom order first, then by product name (Default)
* 'popularity' – by the number of sales
* 'rating' – by the average rating
* 'date' – recently added products will be displayed first
* 'price' – cheapest products will be displayed first
* 'price-desc' – the most expensive first
* 'rand' – in a random order
*
* @param $sort_by
* @return string
*/
function zpd_wc_default_shop_page_orderby( $sort_by ): string {
/**
* You can add conditions to this function - example changing the default ordering of a specific category
*/
if( is_product_category( 'nicknacks' ) ) {
return $sort_by; // no changes for nicknacks category
}
return 'price-desc';
}
add_filter('woocommerce_default_catalog_orderby', 'zpd_wc_default_shop_page_orderby', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment