Created
November 22, 2024 16:15
-
-
Save daveloodts/bab5f9a00808d5efe90058a7d6beae3a to your computer and use it in GitHub Desktop.
Sort on stock in woocommerce
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
// Add a new sorting option for pushing out-of-stock products last | |
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_sort_out_of_stock_last', 9999 ); | |
function custom_sort_out_of_stock_last( $args ) { | |
// Check if the custom sorting option is selected | |
if ( isset( $_GET['orderby'] ) && $_GET['orderby'] === 'instock_last' ) { | |
$args['orderby'] = 'meta_value'; | |
$args['meta_key'] = '_stock_status'; | |
$args['order'] = 'ASC'; // 'instock' comes before 'outofstock' | |
} | |
return $args; | |
} | |
// Add the sorting option to the dropdown | |
add_filter( 'woocommerce_default_catalog_orderby_options', 'add_instocks_last_sorting_option' ); | |
add_filter( 'woocommerce_catalog_orderby', 'add_instocks_last_sorting_option' ); | |
function add_instocks_last_sorting_option( $options ) { | |
$options['instock_last'] = __( 'Sort by stock status', 'woocommerce' ); | |
return $options; | |
} | |
// Modify the product query to respect stock status | |
add_filter( 'woocommerce_product_query_meta_query', 'include_out_of_stock_meta_query', 9999, 2 ); | |
function include_out_of_stock_meta_query( $meta_query, $query ) { | |
if ( isset( $_GET['orderby'] ) && $_GET['orderby'] === 'instock_last' ) { | |
$meta_query[] = array( | |
'key' => '_stock_status', | |
'compare' => 'EXISTS', | |
); | |
} | |
return $meta_query; | |
} | |
// Remove the default sorting dropdown | |
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 ); | |
// Add a custom sorting list | |
add_action( 'woocommerce_before_shop_loop', 'custom_sorting_list', 30 ); | |
function custom_sorting_list() { | |
// Define sorting options | |
$sorting_options = array( | |
'instock_last' => __( 'Op voorraad', 'woocommerce' ), | |
'menu_order' => __( 'Standaard volgorde', 'woocommerce' ), | |
'popularity' => __( 'Populariteit', 'woocommerce' ), | |
'date' => __( 'Nieuwste', 'woocommerce' ), | |
'price' => __( 'Prijs laag > hoog', 'woocommerce' ), | |
'price-desc' => __( 'Prijs hoog > laag', 'woocommerce' ), | |
); | |
// Get the current sorting option | |
$current_orderby = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : 'menu_order'; | |
// Output the sorting list | |
echo '<h4>Sorteer producten op:</h4>'; | |
echo '<ul class="custom-sorting-list">'; | |
foreach ( $sorting_options as $value => $label ) { | |
$active_class = $current_orderby === $value ? 'active' : ''; | |
$url = add_query_arg( 'orderby', $value, remove_query_arg( 'paged' ) ); | |
echo '<li class="' . esc_attr( $active_class ) . '">'; | |
echo '<a href="' . esc_url( $url ) . '">' . esc_html( $label ) . '</a>'; | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment