• Resolved andygroove

    (@andygroove)


    Hello
    I try to use function for order by stock status in my store catalog but this code drop ACF get_field() function
    when function on, get_field return only ID of post (https://skr.sh/sGyfgZ3GwaP)
    and when function off, all right, get_field return array (https://skr.sh/sGyHPC9UUOw)
    please help me to find a reason
    or how to order by stock status

    add_filter( 'posts_clauses', 'order_by_stock_status', 2000 );
    function order_by_stock_status( $posts_clauses ) {
    global $wpdb;
    
    if ( is_woocommerce() && ( is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() ) ) {
        $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ( $wpdb->posts.ID = istockstatus.post_id ) ";
        $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
        $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
    }
    return $posts_clauses;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Saif

    (@babylon1999)

    Hello @andygroove,

    I found an easier approach here: https://stackoverflow.com/a/62353255

    It adds an “availability” option on the shop page for ordering products based on stock.


    Link to image: https://d.pr/i/KbDdBG

    If you want to change the “Availability” string, you can edit the following part ( 'Availability', in $orderby['availability'] = __( 'Availability', 'woocommerce' );.

    Hope this helps!

    Funny you should post this, I just finished debugged this issue on a client site.

    add_filter('posts_clauses', 'sem_order_by_stock_status');
    function sem_order_by_stock_status($posts_clauses) {
        global $wpdb;
        // only change query on WooCommerce loops when ACF fields are not requested in the WHERE statement
        if (!is_acf_request($posts_clauses['where']) && is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
            $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
            $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
            $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
        }
        return $posts_clauses;
    }
    
    function is_acf_request($text){
    	if (strpos($text, 'acf-field'))
    		return true;
    	return false;
    }

    I can think of some edge cases where this would need to be further optimized but this should work for 99.999% of sites out there.

    Thread Starter andygroove

    (@andygroove)

    jake4u thank you so much. Its work for me

    Awesome, @andygroove glad to know it’s working for you. I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

    Cheers!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Order by stock status (posts_clauses) drop ACF get_field function’ is closed to new replies.