gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:36:51 25 4
gpt4 key购买 nike

在 Woocommerce 中,当客户帐单/送货地址是特定城市时,我尝试修改客户订单备注,以显示“通过此提供商发货”。

WordPress 5.2.2WooCommerce 3.6.5

我正在使用 woocommerce_thankyou Hook ,通过订单 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 查询来执行此操作?

编辑:更正回显结果以反射(reflect)代码。

最佳答案

只需添加以下代码片段即可实现任务 -

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

关于php - 通过functions.php 设置 woocommerce 客户注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57420815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com