Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Created May 24, 2016 19:55
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 BFTrick/ea1f316271402442262069af6044b369 to your computer and use it in GitHub Desktop.
Save BFTrick/ea1f316271402442262069af6044b369 to your computer and use it in GitHub Desktop.
Add new option to WooCommerce to sort oldest to newest
<?php
// Add "Sort by date: oldest to newest" to the menu
// We still need to add the functionality that actually does the sorting
// Original credit to Remi Corson: http://www.remicorson.com/woocommerce-sort-products-from-oldest-to-most-recent/
function patricks_woocommerce_catalog_orderby( $sortby ) {
$sortby['oldest_to_recent'] = __( 'Sort by date: oldest to newest', 'woocommerce' );
return $sortby;
}
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