Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify WooCommerce backorder message

So here goes... I have a wordpress site and use woocommerce etc etc. I want to change the backorder message on the product pages to something like “please allow 2 – 4 weeks for delivery of this item” which i’m having issues with.

I also want the same or similar message to show in the shopping cart page where the backorder message usually displays under the item name.

Alternatively, but not my preference, just hide the backorder message from the cart page altogether, but still have it on the product page.

I’m inputting the php code through a plugin rather than using a child theme or mucking about with other things. I usually manage ok, but completely stumped at this. I spent hours on this yesterday and managed to crash my site and everything! lol…

I think it would give a much more professional look to my site.

A link to one of the products on backorder on my site is below so that you can see exactly what i’m looking for.

https://ascentsofscotland.co.uk/product/cormag-celtic-bangle/

Any help is greatly appreciated with this as i'm beginning to get frustrated with something that should in theory be simple... (I think it should actually be a customisable part in woocommerce anyway!)

Thanks in advance to all you genius people out there!

Stu

like image 971
Stu Avatar asked Dec 01 '22 12:12

Stu


2 Answers

I think you need to filter the woocommerce_get_availability_text. Add this to your theme's functions.php or to a site-specific "snippets" plugin.

function so_42345940_backorder_message( $text, $product ){
    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
        $text = __( 'Please allow 2 – 4 weeks for delivery of this item', 'your-textdomain' );
    }
    return $text;
}
add_filter( 'woocommerce_get_availability_text', 'so_42345940_backorder_message', 10, 2 );
like image 167
helgatheviking Avatar answered Dec 05 '22 22:12

helgatheviking


Here's an all in one that works in both the product page and the cart (I use the free "Code Snippets" plugin to handle this instead of messing with functions.php files):

function alt_message() {
  return "This item may take 3-4 weeks to deliver";
}

function backorder_text($availability) {
$altmessage = alt_message();
foreach($availability as $i) {
$availability = str_replace('Available on backorder', $altmessage, $availability);
}
return $availability;
} 
add_filter('woocommerce_get_availability', 'backorder_text');


function woocommerce_custom_cart_item_name( $_product_title, $cart_item, $cart_item_key ) {
$altmessage = alt_message();
  if ( $cart_item['data']->backorders_require_notification() && $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$_product_title .=  __( ' - '. $altmessage, 'woocommerce' ) ;
}
return $_product_title;
}
add_filter( 'woocommerce_cart_item_name', 'woocommerce_custom_cart_item_name', 10, 3);

Also add the following CSS code in the style.css file of your child theme

.backorder_notification{
display: none;
}
.backorder_notification.custom{
display: block;
}

PS: If you don’t add CSS code then it will not work.

credit to: http://www.northmore.net/change-backorder-text-woocommerce/

like image 36
anon Avatar answered Dec 05 '22 23:12

anon