In Woocommerce I’m trying to modify the Customer Order note to say “Ship with this provider” when a customer billing/shipping address is a specific city.
WordPress 5.2.2
WooCommerce 3.6.5
I’m using the woocommerce_thankyou
hook, getting the order data via order ID and get_customer_note()
, set_customer_note()
.
add_action('woocommerce_thankyou','route_mail_on_customer_location', 30, 1);
function route_mail_on_customer_location($order_id){
$CP_cities = ["City 1", "City 2", "City 3"];
$order = wc_get_order( $order_id );
$curr_note = $order->get_customer_note();
echo "<p>Customer note: " . $curr_note . "</p>";
if((strpos($curr_note, "Ship with Canada Post.") == false) && (in_array($order->get_shipping_city(), $CP_cities, true))){
$note = __($curr_note . ". Ship with Canada Post.");
$order->set_customer_note($note);
}
echo "<p>Customer note: " . $order->get_customer_note() . "</p>";
}
The echo results display correctly.
Customer note: Test Note
Customer note: Test Note Ship with Canada Post.
When I check the order page the order note is only the original customer note.
Customer provided note:
Test Note
It looks like the setter isn’t sending the changes to the database. Is there a method I need to call to make sure that my changes are added to the DB, or should I just do it directly via a wpdb query?
EDIT: Corrected echo results to reflect code.
2
Answers
Please use the following code
Just add the follows code snippet to achieve to task –