WooCommerce replace "Available on backorder" in cart/checkout based on product category WooCommerce replace "Available on backorder" in cart/checkout based on product category wordpress wordpress

WooCommerce replace "Available on backorder" in cart/checkout based on product category


Use: woocommerce_cart_item_backorder_notification

Note that the third parameter ($product_id) is specified by has_term, this is because default the current post(ID) is used. However, if there are several products in the shopping cart, there are multiple IDs...

// Change backorder notification - Single product pagefunction custom_availability_text( $text, $product ) {    // Returns whether or not the product is stock managed.    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {        // Check if the current post has any of given terms.        if( has_term( 'bridal-line', 'product_cat' ) ) {            $text = __( 'My first text', 'woocommerce' );        } else {            $text = __( 'My second text', 'woocommerce' );        }    }    return $text;}add_filter( 'woocommerce_get_availability_text', 'custom_availability_text', 10, 2 );// Change backorder notification - Shop pagefunction custom_cart_item_backorder_notification( $html, $product_id ){    // Check if the current post has any of given terms.    if ( has_term( 'bridal-line', 'product_cat', $product_id ) ) {        $html = '<p class="backorder_notification">' . esc_html__( 'My first text', 'woocommerce' ) . '</p>';    } else {        $html = '<p class="backorder_notification">' . esc_html__( 'My second text', 'woocommerce' ) . '</p>';    }    return $html;}add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );


This code in my theme's function.php works for me:

function change_specific_availability_text( $availability ) {    $targeted_text = __( 'Available on backorder', 'woocommerce' );    if ($availability[ 'class' ] == 'available-on-backorder' && $availability[ 'availability' ] == $targeted_text) {        $availability[ 'availability' ] = __( 'In the central warehouse', 'your-theme-textdomain' );    }    return $availability;}add_filter( 'woocommerce_get_availability', 'change_specific_availability_text', 20, 1 );

or alternative, simplified way:

function change_specific_availability_text( $availability ) {    if ($availability[ 'class' ] == 'available-on-backorder') {        $availability[ 'availability' ] = __( 'In the central warehouse', 'your-theme-textdomain' );    }    return $availability;}add_filter( 'woocommerce_get_availability', 'change_specific_availability_text', 20, 1 );