Home Forums WoodMart support forum Sending products with the “outofstock” status to the end of the list

Sending products with the “outofstock” status to the end of the list

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #543171

    Chingiz
    Participant

    Hello!

    Please tell me how you can install a product with the status “outofstock” at the end of the list of products displayed through the widget “products (grid or carousel)”.

    The list of products is created on an arbitrary page. Products with the “New” status are displayed. We want to move products with the status “outofstock” at the end of the list of products and leave products with the status “in stock” at the top of the list of products

    #543240

    Hello

    Please add the below code to the functions.php file into the Child theme. This code shows your out-of-stock products at the end of the archive pages.

    if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
        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())) {
    	$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;
    }

    Best Regards

    #543249

    Chingiz
    Participant

    Thanks for the code provided. It works on the “Shop” page, but it doesn’t work on arbitrary pages, like here. On this page, I inserted the “products (grid or carousel)” widget and displayed products with the “New” badge. Unfortunately for this page, your code did not work.

    #543484

    Hello,

    Yes, this code works only on the archive pages. Unfortunately, there is no widget in this widget to show the “Out of stock” product which you want. Try to change the order of the products in the grid or carousel widget:
    Screenshot for clarification:
    https://ibb.co/NV2PK8r

    Best Regards.

    #543538

    Chingiz
    Participant

    It is a pity that you did not help me with this task. Can I count on you to tell me where this widget is being created? (Class or function)

    #543544

    Chingiz
    Participant

    I found a solution from a wonderful person. I’ll leave it here, in case someone needs it.

    add_filter( 'woocommerce_get_catalog_ordering_args', 'truemisha_sort_by_stock', 25 );
     
    function truemisha_sort_by_stock( $args ) {
     
    	$args[ 'meta_key' ] = '_stock_status';
    	$args[ 'orderby' ] = 'meta_value';
    	$args[ 'order' ] = 'ASC';
     
    	return $args;
     
    }
    #543754

    Hello,

    You are Most Welcome.

    We are glad that you managed to solve the problem yourself. You are Great!!!

    Let us know if there’s anything else we can do for you! You can always reach us at any time. We are always here to help you.

    Have a wonderful day.

    Topic Closed.
    Best Regards.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Sending products with the “outofstock” status to the end of the list’ is closed to new replies.