• Resolved iscampos

    (@iscampos)


    Hello! I’d like some help with this: I’ve been trying for a long time to get out of stock products to appear at the bottom of every category in my store. But I can’t. I already read several threads in the forum and the same code appears that breaks my web page. I put it directly in the functions.php file and also tried it from the code snippets plugin, and it doesn’t work. It always breaks my website.

    I have templates in the “theme generator” for my store… maybe that’s why the code doesn’t work.

    I need to know if there is another way to do it. Thank you very much for the help.

    I copy the code that appears everywhere:

    add_filter('posts_clauses', 'order_by_stock_status');
    function order_by_stock_status($posts_clauses) {
        global $wpdb;
        // only change query on WooCommerce loops
        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;
    }

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi there @iscampos ??

    Thank you for contacting Woo support!

    Products out of stock at the end

    I’ve been trying for a long time to get out of stock products to appear at the bottom of every category in my store.

    I have templates in the “theme generator” for my store… maybe that’s why the code doesn’t work.

    I need to know if there is another way to do it.

    I understand that you’d like to display out of stock products at your store, and have them appearing at the bottom of every category page.

    My recommendation would be to check out the Products block, and with the help of it, along with that of the Products by Category block, create bespoke category pages that suit your needs.

    Below I’m attaching a screenshot of the options of the Products block currently at my installation, or reference:

    Direct link to image: https://snipboard.io/1iE4Cq.jpg

    I hope this is helpful! Please let us know if you have any further questions or concerns.

    Thread Starter iscampos

    (@iscampos)

    My query was not understood ?? I don’t want to place the out-of-stock products in the “bottom” part. I want the out-of-stock products to always appear at the end of each category when ordering the store. There are codes on the internet that do this but they don’t work on my site. They break my site and I think it may be because I use the Divi theme. Can you help me to achieve this?

    I didn’t really understand your recommendation… can I find the product block in Widgets? because I think this solution doesn’t work for me… ??

    Howdy @iscampos

    Thank you for reaching back, clarifying things further.

     I want the out-of-stock products to always appear at the end of each category when ordering the store. 

    My suggestion would be to check out the snippet linked here, for displaying products in stock first.

    Additionally, for changing the default ordering with code, feel free to check out the code snippet linked here.

    Just to note, the Code Snippets plugin is a good choice for adding such code snippets to your site.

    There are codes on the internet that do this but they don’t work on my site. They break my site and I think it may be because I use the Divi theme. 

    For making sure any additional code to your site does not create conflict with the currently active theme, feel free to temporarily switch to another, like Storefront, and see how it goes.

    Please let us know if you have any further questions about this matter, or if we misinterpret your concern in any way. We will be happy to help you further.

    Thread Starter iscampos

    (@iscampos)

    Hello! First of all, thank you because I have been trying to order the products in my store for a long time, by quantity of inventory (stock) and all the codes that are on the internet to bring the sold-out products to the end, they broke my site and they did not work for me. This has helped me a lot!! now I was able to set the order by stock option by default ??

    But I want to ask you for help with something… I don’t know how to modify the code so that it also works on the main “shop” page. It works perfectly in all categories but in the “store” it does not order the products and it shows a 404 error page when trying to order.

    The “shop” page of my site where the filter doesn’t work is: https://nenufar.cl/tienda/

    The code I added is:

    //order by stock
    
    add_filter( 'woocommerce_catalog_orderby', 'misha_add_custom_sorting_options' );
    
    function misha_add_custom_sorting_options( $options ){
    	
    	$options[ 'title' ] = 'Ordenar alfabéticamente';
    	$options[ 'in-stock' ] = 'Ordenar por stock';
    	
    	return $options;
    }
    
    add_filter( 'woocommerce_get_catalog_ordering_args', 'misha_custom_product_sorting' );
    function misha_custom_product_sorting( $args ) {
    	
    	
    	// Sort alphabetically
    	if ( isset( $_GET[ 'orderby' ] ) && 'title' === $_GET[ 'orderby' ] ) {
    		$args[ 'orderby' ] = 'title';
    		$args[ 'order' ] = 'asc';
    	}
    	
    	// Show products in stock first
    	if( isset( $_GET[ 'orderby' ] ) && 'in-stock' === $_GET[ 'orderby' ] ) {
    		$args[ 'meta_key' ] = '_stock_status';
    		$args[ 'orderby' ] = array( 'meta_value' => 'ASC' );
    	}
    	
    	return $args;
    }

    I really appreciate all your help so much ??

    Thread Starter iscampos

    (@iscampos)

    I just noticed another problem ?? the new filters work only on the web, but woocommerce and Divi theme don’t recognize them. I tried to modify the templates of the shop categories so that they would sort by stock immediately when you click on a category but the new order options don’t show up in any woocommerce or divi settings ??

    Saif

    (@babylon1999)

    Hello @iscampos,

    Glad to know there’s some progress!

    ut I want to ask you for help with something… I don’t know how to modify the code so that it also works on the main “shop” page. It works perfectly in all categories but in the “store” it does not order the products and it shows a 404 error page when trying to order.

    Apparently, your shop page t is not recognized as an archive page. I was able to confirm this from your site’s source code, as the .archive class is not available on the shop page.

    Meanwhile on category pages:


    Link to image: https://d.pr/i/4gCaYr

    You can check if tienda is assigned as the shop page from WooCommece > Settings > Products:


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

    I just noticed another problem ?? the new filters work only on the web, but woocommerce and Divi theme don’t recognize them. I tried to modify the templates of the shop categories so that they would sort by stock immediately when you click on a category but the new order options don’t show up in any woocommerce or divi settings ??

    This forum is for WooCommerce-core-related topics so the contributors here might not be able to provide assistance for this part. Additionally, debugging third-party integrations is not within our scope of support.

    I suggest you get in touch with Elegant Themes for further assistance, here’s their support page: https://www.elegantthemes.com/support/

    Cheers!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Products out of stock at the end’ is closed to new replies.