Home > Mobile >  Add custom stock status in WooCommerce treated as out of stock
Add custom stock status in WooCommerce treated as out of stock

Time:06-23

I created 2 new custom stock statuses:

  1. Out of stock (Permanent)
  2. Out of stock (Supplier)

As you can guess, I want them both to be treated as out of stock.


I used some extra code to hide the products with these custom statuses from the front end but I can not hide them from the ajax search (live) and also someone can add the product in cart.

(I use the Flatsome theme and the live search is from there)

Based on: custom stock status


Step 2) when the product is 'out of stock', we will check whether a custom stock status has been set. If this is the case, we will visually change the text using the code below, so that the customer can see the new status. In the background/backend, however, the 'out of stock' status is still used and therefore automatically also the existing functionality:

// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
    // Only for 'outofstock'
    if ( $product->get_stock_status() == 'outofstock' ) {
        // Get custom stock status
        $custom_stock_status = $product->get_meta( '_custom_stock_status' );

        // Compare and apply new text
        if ( $custom_stock_status == 'permanent' ) {
            $availability = __( 'Out of stock (Permanent)', 'woocommerce' );
        } elseif ( $custom_stock_status == 'supplier' ) {
            $availability = __( 'Out of stock (Supplier)', 'woocommerce' );
        }
    }

    return $availability; 
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
  • Related