Created
July 8, 2015 08:43
-
-
Save AchalJ/144785406b03072f0a32 to your computer and use it in GitHub Desktop.
WooCommerce custom function for Sort by Stock
This file contains hidden or 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
| // 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