• Hello. Is it possible to organize the search results by stock status.

    – First the products in stock.

    – Second, the backorder products.

    – Lastly the products out of stock.

    What would be the code to add or modify to achieve this?

    Thank you for your help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author ILLID

    (@mihail-barinov)

    Hello,

    You can try to use following code snippet

    add_filter( 'aws_search_results_products', 'aws_search_results_products' );
    function aws_search_results_products( $products ) {
        usort($products, function ($item1, $item2) {
            if ( isset( $item1['stock_status']['status'] ) && isset( $item2['stock_status']['status'] ) ) {
                if ( $item1['stock_status']['status'] ) {
                    return -1;
                } else if ( $item2['stock_status']['status'] ) {
                    return 1;
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        });
        return $products;
    }
    Thread Starter audiomag

    (@audiomag)

    Hello. I already have this active code in my function.php file.

    Unfortunately it does not work.

    Here is my function.php file :

    <?php
    add_action( ‘wp_head’, ‘add_my_google_analytics_code’, PHP_INT_MAX );
    function add_my_google_analytics_code() {
    ?>
    <!– Global site tag (gtag.js) – Google Analytics –>
    <script async src=”https://www.googletagmanager.com/gtag/js?id=UA-4216156-1″></script&gt;
    <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag(‘js’, new Date());

    gtag(‘config’, ‘UA-4216156-1’);
    </script>

    <?php
    }

    /**
    * RENOMER LA TAB ??MARQUE??
    */
    add_filter( ‘woocommerce_product_tabs’, ‘woo_rename_tabs’, 98 );
    function woo_rename_tabs( $tabs ) {

    $tabs[‘pwb_tab’][‘title’] = __( ‘Marque’ ); // Rename the description tab

    return $tabs;

    }

    /**
    * AFFICHER LES PRODUITS EN STOCK EN PREMIER V2.
    */

    add_action( ‘woocommerce_product_query’, ‘bbloomer_sort_by_stock_status_then_alpha’, 999 );

    function bbloomer_sort_by_stock_status_then_alpha( $query ) {
    if ( is_admin() ) return;
    $query->set( ‘meta_key’, ‘_stock_status’ );
    $query->set( ‘orderby’, array( ‘meta_value’ => ‘ASC’ ) );
    }

    /**
    * CHANGER LE NOMBRE DE PRODUITS APPARENTES
    */
    function woo_related_products_limit() {
    global $product;

    $args[‘posts_per_page’] = 6;
    return $args;
    }
    add_filter( ‘woocommerce_output_related_products_args’, ‘jk_related_products_args’, 20 );
    function jk_related_products_args( $args ) {
    $args[‘posts_per_page’] = 6; // 6 related products
    $args[‘columns’] = 3; // arranged in 2 columns
    return $args;
    }

    /**
    * FILTRE MOTEUR DE RECHERCHE – BACKORDER
    */

    add_filter( ‘aws_search_pre_filter_products’, ‘my_aws_search_pre_filter_products’ );
    function my_aws_search_pre_filter_products( $products_array ) {
    if ( $products_array && ! empty( $products_array ) ) {
    foreach ( $products_array as $key => $product ) {
    if ( $product[‘stock_status’] && isset( $product[‘stock_status’][‘status’] ) && $product[‘stock_status’][‘status’]) {

    $product_obj = wc_get_product( $product[‘id’] );

    if ( $product_obj->is_on_backorder() ) {
    $products_array[$key][‘stock_status’][‘text’] = ‘<span style=”color:#eaa600;”>’ . esc_html__( ‘Contactez nous’, ‘advanced-woo-search’ ) . ‘</span>’;
    }

    }
    }
    }
    return $products_array;
    }

    /**
    * RELEGER EN FIN DE RECHERCHE LES PRODUITS OUT OF STOCK DANS WOOCOMMERCE RECHERCHE DYNAMIQUE
    */

    add_filter( ‘aws_search_results_products’, ‘aws_search_results_products’ );
    function aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
    if ( isset( $item1[‘stock_status’][‘status’] ) && isset( $item2[‘stock_status’][‘status’] ) ) {
    if ( $item1[‘stock_status’][‘status’] ) {
    return -1;
    } else if ( $item2[‘stock_status’][‘status’] ) {
    return 1;
    } else {
    return 0;
    }
    } else {
    return 0;
    }
    });
    return $products;
    }

    Plugin Author ILLID

    (@mihail-barinov)

    Did you try to clear plugin cache after adding this custom code?

    Thread Starter audiomag

    (@audiomag)

    Yes i have clear all cache, plugin and wordpress and “Cache results” option is off.

    • This reply was modified 5 years, 2 months ago by audiomag.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to sorting results by stock status’ is closed to new replies.