Skip to content

Instantly share code, notes, and snippets.

@mrmoyeez
Forked from EricBusch/sort-by-discount.php
Created December 9, 2019 06:53
Show Gist options
  • Save mrmoyeez/108cefa231c1c182570958001ebc909b to your computer and use it in GitHub Desktop.
Save mrmoyeez/108cefa231c1c182570958001ebc909b to your computer and use it in GitHub Desktop.
Add "Sort by discount" to sorting options. Defaults to biggest to smallest discount.
<?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