I have a custom post called "project", and it has an acf field called "ponum".
When a customer purchases a product, I'd like to add the customer's order number to the "ponum" field of the user’s post in the custom post "project".
Getting an order number & creating a post works, but I have no idea how to add this number to the acf field on a thank you page.
add_action( 'woocommerce_thankyou', 'create_post_on_order' );
function create_post_on_order($order_id)
{
global $wpdb;
//print('<pre>'.print_r( $wpdb, true ).'</pre>');
$order = wc_get_order($order_id);
if (!$order->get_id()) {
return;
}
$current_user = wp_get_current_user();
$current_user_id = $current_user->display_name;
$post_title = $order_id . ' - ' . $current_user_id;
// Check post already exit with our title or not
$query = $wpdb->prepare(
'SELECT ID FROM ' . $wpdb->posts . '
WHERE post_title = %s
AND post_type = \'project\'',
$post_title
);
$wpdb->query($query);
if (!$wpdb->num_rows) {
$post_id = wp_insert_post(
array(
'author' => $current_user_id,
'post_title' => $post_title,
'post_status' => 'publish',
'post_type' => 'project'
)
);
}
}
Would you please help me?
Thank you.