Created
February 4, 2014 21:09
-
-
Save EricBusch/8812440 to your computer and use it in GitHub Desktop.
Add "Sort by discount" to sorting options. Defaults to biggest to smallest discount.
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 discount" to sorting options. Defaults to biggest to smallest discount. | |
*/ | |
add_filter( 'woocommerce_get_catalog_ordering_args', 'mycode_woocommerce_add_salediscount_to_catalog_ordering_args' ); | |
function mycode_woocommerce_add_salediscount_to_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 ( 'discount' == $orderby_value ) { | |
$args['orderby'] = 'meta_value_num'; | |
$args['order'] = 'DESC'; | |
$args['meta_key'] = '_dfrps_salediscount'; | |
} | |
return $args; | |
} | |
add_filter( 'woocommerce_default_catalog_orderby_options', 'mycode_woocommerce_add_salediscount_to_catalog_orderby' ); | |
add_filter( 'woocommerce_catalog_orderby', 'mycode_woocommerce_add_salediscount_to_catalog_orderby' ); | |
function mycode_woocommerce_add_salediscount_to_catalog_orderby( $sortby ) { | |
$sortby['discount'] = 'Sort by discount'; | |
return $sortby; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment