Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.