Skip to content

Instantly share code, notes, and snippets.

@AchalJ
Created July 8, 2015 08:43
Show Gist options
  • Select an option

  • Save AchalJ/144785406b03072f0a32 to your computer and use it in GitHub Desktop.

Select an option

Save AchalJ/144785406b03072f0a32 to your computer and use it in GitHub Desktop.
WooCommerce custom function for Sort by Stock
// Sort by stock on front end
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_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 ( 'stock' == $orderby_value ) {
$args['orderby'] = 'meta_value';
$args['order'] = 'asc';
$args['meta_key'] = '_stock_status';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['stock'] = 'Stock Status';
return $sortby;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment