This code works, it adds a note to the order if the customer buys a product which is in a conditional category as well as other products outside of that category and the order is moved to processing. The message is that the customer will receive 2 packages since they ship from different locations. However, I want it to add the message as a customer message rather than a note, but I can't get it to work.
I tried with
$order->set_customer_note($customer_note);
$order->save();
but it doesn't work.
My code attempt:
function add_custom_note_to_order($order_id) {
$order = wc_get_order( $order_id );
// Initialize categories array
$categories = array();
// Loop through order items
foreach($order->get_items() as $item) {
$product_id = $item->get_product_id();
$terms = get_the_terms($product_id, 'product_cat');
foreach($terms as $term) {
$categories[] = $term->slug;
}
}
// Categories to check
$check_categories = array('accessories-1', 'shirtsblue', 'shirtsred', 'shirtsgreen');
// Check if order contains products from the cat above
// and also from another category
if(array_intersect($check_categories, $categories) && sizeof(array_diff($categories, $check_categories)) > 0) {
$order_note = 'Your order will be shipped in 2 parcels';
$order->add_order_note($order_note);
}
}
add_action('woocommerce_order_status_processing', 'add_custom_note_to_order');
When using WC_Order
add_order_note()
method, there is $is_customer_note
2nd optional argument (boolean), that you need to set it to true
, like:
$order_note = 'Your order will be shipped in 2 parcels';
$order->add_order_note($order_note, true);
Now it should work.