WOOF push out of stock at the end of the list but group them by category

PHOTO EMBED

Wed Mar 16 2022 05:45:52 GMT+0000 (Coordinated Universal Time)

Saved by @zildjianjaxolis #wordpress #woocommerce #woof

<?php
/**
 * IMPORTANT!!!
 * Before anything else, make sure that you created a custom field for category and has a field called 'custom_order'
 * Then on each catogory, fill out the custom_order field.
 */

/**
* Separate file
* Add a $is_oos_last variable before the woof shortcode then set it to false after the shortcode
*/
?>

<?php $is_oos_last = true; ?>
 <?php echo do_shortcode( '[woof_products per_page="12" columns="3" is_ajax="1"]' ); ?>
<?php $is_oos_last = false; ?>

<?php
/**
* functions.php
* Modify the query to push out of stock at the bottom of the list
*/
add_filter('posts_clauses', 'tbd_order_by_stock_status', 9999);
function tbd_order_by_stock_status($posts_clauses)
{
    global $wpdb;
    global $is_oos_last;

    if ($is_oos_last == 1 || $_GET['is_oos_last'] == 1 || is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() )) {
        $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
        $posts_clauses['join'] .= " INNER JOIN {$wpdb->prefix}term_relationships term_rel ON {$wpdb->posts}.ID = term_rel.object_id INNER JOIN {$wpdb->prefix}termmeta termmeta1 ON termmeta1.term_id=term_rel.term_taxonomy_id ";
        $posts_clauses['orderby'] = " termmeta1.meta_value ASC, istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
        $posts_clauses['where'] = " AND term_rel.term_taxonomy_id IN (SELECT term_id FROM {$wpdb->prefix}term_taxonomy WHERE taxonomy='product_cat') AND termmeta1.meta_key = 'custom_order' AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
    }
    return $posts_clauses;
}
add_filter('woof_draw_products_get_args', 'tbd_woof_draw_products_get_args', 9999 );
function tbd_woof_draw_products_get_args( $args ) {
    $args['is_oos_last'] = 1;
    return $args;
}
content_copyCOPY