woocommercestock

How to get "available on backorder" text to show after some customization of stock status in woo commerce


I have some customization for the stock status for my products in my woocommerce store.

It looks like this:

add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
function bbloomer_custom_get_availability_text( $availability, $product ) {
$stock = $product->get_stock_quantity();
if ( $product->is_in_stock() && $stock < 19 ) {
    if ($stock == 1) { 
  
    $availability = " " . $stock . " i lager";
}
return $availability;
}

The problem i have is that the "available on backorder text" is not showing when i use this code (when it is enabled in the products stock settings). Now it just says O in stock, so you miss that is possible to backorder the actual product.

So is it a way to get same display for stock status, and also the text "Out of stock" and (if enabled) "Available on backorder?"

I have tried to add this code, but it didnt change anything.

    add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
    function filter_product_availability_text( $availability_text, $product ) {
    // Check if product status is on backorder
    if ($product->get_stock_status() === 'onbackorder') {
        $availability_text = __( 'Kan restnoteras', 'your-text-domain' );
    }
    return $availability_text;
    }

Would be happy if someone have a good advice here!


Solution

  • Updated - Try the following:

    add_filter( 'woocommerce_get_availability_text', 'custom_availability_text', 100, 2 );
    function custom_availability_text( $availability, $product ) {
        if ( ! $product->is_in_stock() ) {
            $availability = __('Ikke på lager', 'woocommerce' );
        } elseif ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
            $availability = $product->backorders_require_notification() ? __('Kan restnoteras', 'woocommerce' ) : '';
        } elseif ( ! $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
            $availability = __('Kan restnoteras', 'woocommerce' );
        } elseif ( $product->managing_stock() ) {
            $availability = __( 'I lager', 'woocommerce' );
            $stock_amount = $product->get_stock_quantity();
    
            switch ( get_option( 'woocommerce_stock_format' ) ) {
                case 'low_amount':
                    if ( $stock_amount <= wc_get_low_stock_amount( $product ) ) {
                        $availability = sprintf( __( 'Bare %s igjen på lager' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
                    }
                    break;
                case '':
                    $availability = sprintf( __( '%s i lager', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
                    break;
            }
            if ( $product->backorders_allowed() && $product->backorders_require_notification() ) {
                $availability .= ' ' . __( '(kan restbestilles)', 'woocommerce' );
            }
        } else {
            $availability = '';
        }
        return $availability;
    }
    

    It should work.