Last active
March 8, 2021 23:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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