Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Created May 24, 2016 20:12
Show Gist options
  • Save BFTrick/877860c445d759433442423631e6c7e0 to your computer and use it in GitHub Desktop.
Save BFTrick/877860c445d759433442423631e6c7e0 to your computer and use it in GitHub Desktop.
Customize the sort by drop down in WooCommerce
<?php
function patricks_woocommerce_catalog_orderby( $orderby ) {
// Add "Sort by date: oldest to newest" to the menu
// We still need to add the functionality that actually does the sorting
$orderby['oldest_to_recent'] = __( 'Sort by date: oldest to newest', 'woocommerce' );
// Change the default "Sort by newness" to "Sort by date: newest to oldest"
$orderby["date"] = __('Sort by date: newest to oldest', 'woocommerce');
// Remove price & price-desc
unset($orderby["price"]);
unset($orderby["price-desc"]);
return $orderby;
}
add_filter( 'woocommerce_catalog_orderby', 'patricks_woocommerce_catalog_orderby', 20 );
// Add the ability to sort by oldest to newest
function patricks_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'oldest_to_recent' == $orderby_value ) {
$args['orderby'] = 'date';
$args['order'] = 'ASC';
}
return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'patricks_woocommerce_get_catalog_ordering_args', 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment