A client walks into a bar and asks me “can I change the backorder message in woocommerce?”
I reply: “Of course! 😀”
Setup
The code in this tutorial pre-setup with wordpress & woocommerce installed. I am using a an empty child theme of storefront.
Download this repository: for a quick start bare-bones storefront child theme with minimal configuration.
prettier & prettier-php
I use prettier & prettier-php to automatically make my code prettier and its installed in the theme linked above. (Remember to run: npm install
)
Adding the filter
In wordpress, a “adding a filter” is essentially adding function to an array of functions which will be performed at a specific spot of the code.
Add_filter()
Learn more about add_filter() at the official wordpress documentation.
Parameters
add_filter()
takes four parameters, a name, a function to perform, a priority and its expected number of parameters.
- Name – The name of the action where the functions will execute
- Function – The name of the function we are adding to the pipeline
- Priority – The order in which the functions run (ASC)
- Number of parameters – How many parameters your function is expecting
<?php
add_filter(
"woocommerce_get_availability_text",
"gregbastianelli_change_backorder_message",
10,
2
);
So above: we are calling the ‘woocommerce_get_availability_text'
action, we are adding our ‘gregbastianelli_change_backorder_message
‘ function to the pipeline, setting a priority of 10
and expecting 2
parameters.
Change backorder message globally
If we visit the source code for woocommerce_get_availability_text you can see it returns two parameters in the apply_filters
function.
Expected Parameters
$availability
is the text message returned when a product shows its on backorder.$this
is the product in question.
Now that we know which data is being passed to us – we can setup our function.
Step-by-step
Setup up the function – write documentation and do stuff.
function gregbastianelli_change_backorder_message($text, $product)
{
/**
* Allows developer to change backorder message site-wide
* @param string $text
* @param WC_Product $product
*/
// do stuff
}
First we want to set a backorder message.
$backorder_message = "Will ship in January 2021";
Next we want to set a condition so our backorder message only shows on products on backorder.
...
if ($product->managing_stock() &&
$product->is_on_backorder(1)) {
return $backorder_message;
}
return $text;
}
We also include a return under our condition so the work done by the previous functions in the pipeline is passed on if our condition is not met.
The end results (tl;dr)
In a hurry? Here are the end results.
How to change backorder message store wide
<?php
add_filter(
"woocommerce_get_availability_text",
"gregbastianelli_change_backorder_message",
10,
2
);
function gregbastianelli_change_backorder_message($text, $product)
{
/**
* Allows developer to change backorder message site-wide
* @param string $text
* @param WC_Product $product
*/
$backorder_message = "Will ship in January 2021";
if ($product->managing_stock() &&
$product->is_on_backorder(1)) {
return $backorder_message;
}
return $text;
}
How to change the backorder message for a specific product
If you want to have different messages for different products, include a check for a specific id as well.
...
$backorder_message = "Will ship in January 2021";
$target_id = 272; // include ID
if (
$target_id === $product->get_id() && // check for ID
$product->managing_stock() &&
$product->is_on_backorder(1)
) {
return $backorder_message;
}
return $text;
}
You did it!
Look at you. Changing your very own backorder message in woocommerce like a DEV PRO!