简体   繁体   English

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

[英]Setting woocommerce customer note through functions.php not working

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.在 Woocommerce 中,当客户账单/送货地址是特定城市时,我试图修改客户订单注释以说“与该提供商一起发货”。

WordPress 5.2.2 WooCommerce 3.6.5 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() .我正在使用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>";
}

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.看起来 setter 没有将更改发送到数据库。 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?有没有我需要调用的方法来确保我的更改添加到数据库中,还是应该直接通过 wpdb 查询来执行?

EDIT: Corrected echo results to reflect code.编辑:更正回声结果以反映代码。

Just add the follows code snippet to achieve to task -只需添加以下代码片段即可完成任务 -

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 );

Please use the following code请使用以下代码

add_action( 'woocommerce_email_after_order_table', 
'customer_note_email_after_order_table', 10, 4 ); 
function 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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM