對于特定的用戶角色,我想向訂單添加特定的客戶注釋。
這是我的代碼:
add_action( 'woocommerce_new_order', 'add_customer_note_user_role' );
function add_customer_note_user_role( $order_id ) {
$user_info = get_userdata(get_current_user_id());
if ( $user_info->roles[0]=="administrator" ) {
$order = wc_get_order( $order_id );
// The text for the note
$note = 'This is the message';
// Add the note
$order->add_order_note( $note );
// Save the data
$order->save();
}
}
但這會將訊息放在錯誤的位置。當您在后端檢查訂單時,它會顯示在紫色訊息框中。
我希望訊息顯示為顯示在送貨地址下的客戶備注。因為我的 API 正在從那個地方提取筆記并將它們放在我們的 ERP 中。
我試圖改變
$order->add_order_note( $note );
到
$order->add_order_note( $note, 'is_customer_note', true );
但是沒有想要的結果,有什么建議嗎?
uj5u.com熱心網友回復:
要將訊息顯示為顯示在送貨地址下方的客戶備注,您可以改用set_customer_note()。
所以你得到:
function action_woocommerce_new_order( $order_id ) {
// Get the WC_Order Object
$order = wc_get_order( $order_id );
// Get the WP_User Object
$user = $order->get_user();
// Check for "administrator" user roles only
if ( is_a( $user, 'WP_User' ) && in_array( 'administrator', (array) $user->roles ) ) {
// The text for the note
$note = 'This is the message';
// Set note
$order->set_customer_note( $note );
// Save
$order->save();
}
}
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
要保留原始訊息(當非空時),請改用:
function action_woocommerce_new_order( $order_id ) {
// Get the WC_Order Object
$order = wc_get_order( $order_id );
// Get the WP_User Object
$user = $order->get_user();
// Check for "administrator" user roles only
if ( is_a( $user, 'WP_User' ) && in_array( 'administrator', (array) $user->roles ) ) {
// The text for the note
$note = 'This is the message';
// Get customer note
$customer_note = $order->get_customer_note();
// NOT empty
if ( ! empty ( $customer_note ) ) {
$note = $customer_note . ' | ' . $note;
}
// Set note
$order->set_customer_note( $note );
// Save
$order->save();
}
}
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/355637.html
標籤:WordPress的 求购 后端 订单 用户角色
