• Resolved chatingtou

    (@chatingtou)


    Is there any way to sort “out of stock” products to the end in elementor with shortcode [products]? I have try this code but it is not works in elementor.

    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;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @chatingtou

    Thanks for reaching out!

    I understand that you would like to sort out-of-stock products to be placed at the end of the list when using the Elementor plugin, correct?

    I did some research and found this article could be a good starting point: https://stackoverflow.com/questions/25113581/show-out-of-stock-products-at-the-end-in-woocommerce

    Furthermore, since you are using a third-party plugin that we don’t provide support for based on our Support Policy, I recommend that you reach out to the developers of the plugin for further assistance. They will be better equipped to help you resolve any issues you’re experiencing with the plugin.

    All the best and hopefully, the issue will be fixed soon

    Hi @chatingtou,

    The following snippet of code works well for me when added to my child theme’s (Storefront theme’s child theme) functions.php file with no plugins activated except WooCommerce:

    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;
    }

    Unfortunately, I am unable to add this snippet of code using the Custom Code feature of Elementor as it needs me to upgrade to the paid version of the plugin.

    You can reach out to Elementor Support and they will be able to look into it further.

    It might be helpful to anyone using this code and having problems to note that although this orders the products as intended, it unfortunately breaks my main menu in Divi sites. A non-standard menu is implemented on /shop/ and category pages when this code is in my functions file. There could be a conflict with another plugin or other code etc, or just with Divi.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @umdiecker

    I understand that you’re experiencing issues with your main menu on Divi sites after using this code snippet. It’s possible that there may be a conflict with another plugin or some other code on your site.

    Since Divi is a third-party theme which we don’t provide support for (please see our Support Policy), it would be best to reach out to the theme’s developers for further assistance here. I am sure they will have no problem supporting you there.

    Meanwhile, if you have further inquiries with WooCommerce core, kindly create a new topic here.

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sorting woocommerce out of stock products to the end’ is closed to new replies.