Created
May 24, 2016 19:55
-
-
Save BFTrick/ea1f316271402442262069af6044b369 to your computer and use it in GitHub Desktop.
Add new option to WooCommerce to sort oldest to newest
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
<?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