0

I'm trying to filter the products in the shop page by stock. Below is the code I added in my child theme's functions.php to add 2 new stock statuses. Which is working fine as expected.

function add_custom_stock_type() {
    ?>
    <script type="text/javascript">
    jQuery(function(){
        jQuery('._stock_status_field').not('.custom-stock-status').remove();
    });
    </script>
<?php   

    woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
        'readytoship'     => __( 'Ready to ship', 'woocommerce' ), 
        'outofstock'  => __( 'Out of stock', 'woocommerce' ), 
        'onbackorder' => __( 'Backorder', 'woocommerce' ), 
        'customized'      => __( 'Customized', 'woocommerce' ), 
    ), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
}
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');

function save_custom_stock_status( $product_id ) {
    update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
}
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);

function woo_add_custom_general_fields_save_two( $post_id ){
    // Select
    $woocommerce_select = $_POST['_stock_status'];
    if( !empty( $woocommerce_select ) )
        update_post_meta( $post_id, '_stock_status', esc_attr( $woocommerce_select ) );
    else
    update_post_meta( $post_id, '_stock_status', '' );
    }

function woocommerce_get_custom_availability( $data, $product ) {
    switch( $product->stock_status ) {
        case 'readytoship':
            $data = array( 'availability' => __( 'Ready to ship', 'woocommerce' ), 'class' => 'ready-to-ship' ); 
        break;
        case 'outofstock':
            $data = array( 'availability' => __( 'Out of stock', 'woocommerce' ), 'class' => 'out-of-stock' ); 
        break;
        case 'onbackorder':
            $data = array( 'availability' => __( 'Backorder', 'woocommerce' ), 'class' => 'onbackorder' ); 
        break;
        case 'customized':
            $data = array( 'availability' => __( 'Customized', 'woocommerce' ), 'class' => 'customized' ); //added new one
        break;
    }
    return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);

So this is the code I'm using and I'm getting the stock status dropdown perfectly: https://prnt.sc/vrm5a9

I'm trying to "Filter Products Based on Stock Type" which I'm quite unsure how. I planned to place it in the woocommerce sidebar along with other filters like categories, price slider and stuff.

Any help is appreciated.

  • The code you are using is outdated, see [How to add custom stock status to products in WooCommerce 4.0](https://stackoverflow.com/questions/61796640/how-to-add-custom-stock-status-to-products-in-woocommerce-4-0) to use a shorter and more efficient code. Regarding the following: **_I'm trying to "Filter Products Based on Stock Type" which I'm quite unsure how. I planned to place it in the WC sidebar along with other filters like categories, price slider and stuff._** That is quite broadly defined and your question seems to me to be opinion-based. Can you express this more concretely? – 7uc1f3r Nov 28 '20 at 07:15
  • Thanks for the comment, I already tried the code you mentioned in the link. The option box is not visible if I click on the "Manage Stock at Product Level". Any fix for that? Regarding the filter option, there are a few plugins which allows me to filter "In Stock" products... Similarly I want to filter products based on the stock type i.e Show only "Customized Products" or show only "Ready to ship" products. I hope this is clear. Thanks! – Pavan Kumar Nov 28 '20 at 15:37
  • It is **standard behavior in WooCommerce** that this (stock statuses) is/are not visible when "Manage Stock at Product Level" is clicked/checked. **The code you are using is going to remove the default field and create a new one** while the code I'm referring to **adds the new statuses to the existing dropdown menu**. It is clear, but there are several ways to add filters (widgets, extra plugins, woof, WooCommerce sorting option, etc ..) and that is not clear to me. You do not really indicate which filter it is specifically about. As I have already indicated, your question is too broad – 7uc1f3r Nov 29 '20 at 14:15

0 Answers0