通过functions.php设置woocommerce客户注释不起作用

时间:2019-08-08 21:26:27

标签: php mysql wordpress woocommerce

在Woocommerce中,当客户的帐单/送货地址是特定城市时,我试图将“客户订单”注释修改为“与该提供商的货件”。

WordPress 5.2.2 WooCommerce 3.6.5

我正在使用woocommerce_thankyou挂钩,通过订单ID和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>";
}

回声结果正确显示。

Customer note: Test Note

Customer note: Test Note Ship with Canada Post.

当我检查订单页面时,订单注释只是原始的客户注释。

Customer provided note:
Test Note

setter似乎没有将更改发送到数据库。是否需要调用一种方法来确保将更改添加到数据库中,还是应该直接通过wpdb查询直接完成?

编辑:更正了回声结果以反映代码。

2 个答案:

答案 0 :(得分:1)

只需添加以下代码片段即可完成任务-

function modify_woocommerce_checkout_posted_data( $posted_data ){

    $CP_cities = array( 'city1', 'city2', 'city3' ); // make sure to replace with proper city data

    $curr_note = $posted_data['order_comments'];
    if( strpos($curr_note, 'Ship with Canada Post.') == false  && in_array( $posted_data['shipping_city'], $CP_cities ) ){
        $note = $curr_note . __(' Ship with Canada Post.', 'textdomain' );
        $posted_data['order_comments'] = $note;
    }
    return $posted_data;
}
add_filter( 'woocommerce_checkout_posted_data', 'modify_woocommerce_checkout_posted_data', 99 );

答案 1 :(得分:0)

请使用以下代码

add_action('woocommerce_email_after_order_table','customer_note_email_after_order_table',10,4);

功能customer_note_email_after_order_table($ order,$ sent_to_admin, $ plain_text,$ email){

// Only on some email notifications
if ( in_array( $email->id, array('new_order', 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order') ) ) :

// Get customer Order note
$customer_note = $order->get_customer_note();

// Display the Customer order notes section
echo '<h2>' . __("Order notes", "woocommerce") . '</h2>
<div style="margin-bottom: 40px;">
<table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 2px solid #e5e5e5;" border="0">
    <tr><td><p>' . $customer_note . '</p></td></tr>
</table></div>';

endif;

}