无法在WooCommerce操作中更新ACF字段



由于未知的原因,我的函数不更新ACF自定义字段。该功能是搜索特殊订单备注(我不知道为什么插件开发人员自己没有添加跟踪号到订单元)。

似乎一切都是正确的,但我仍然没有看到更新。我错过了什么?

// add_action( 'woocommerce_after_order_object_save', 'find_tracking', 10, 1); // Also tried, it runs function 3 times
add_action( 'save_post_shop_order', 'pax_find_tracking', 10, 1);
function find_tracking ($order_id) {
global $wpdb;
$order = wc_get_order( $order_id );
$ID = $order->get_id();
$status = $order->get_status();
if ('completed' == $status) { // it's for testing
$table_perfixed = $wpdb->prefix . 'comments';
$sql = "SELECT `comment_content` FROM $table_perfixed WHERE `comment_post_ID` = $ID AND `comment_content` LIKE '%Shipment data sent to server%'";
$result = $wpdb->get_var($sql); // checked, correct unique result 
if ($result) {
$result = mb_substr($result, 44); // checked, correct tracking number 
update_field('my_tracking_field', $result, $ID); // Also tried without $ID
//$order->add_order_note($result); // Checked result — it's string, not empty, $result is fine
}
}
}

没有找到问题的根源,但解决方案是使用update_post_meta代替update_field

update_post_meta( $order_id, 'my_tracking_field', $result );

最新更新