2

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.

Ruvee
  • 8,611
  • 4
  • 18
  • 44
Aino
  • 121
  • 8
  • It's absolutely doable! Have you tried anything? Can you provide us with your code snippet? – Ruvee Apr 03 '21 at 00:59
  • Hello, I can only get the order number (I added the code) and totally have no idea how to add the number to the user's acf field on a thank you page. Could you please help me? – Aino Apr 03 '21 at 04:21

1 Answers1

2

Once you have the value for your acf (i.e "$order_id"), then you could use the following code to update the acf field:

UPDATED ANSWER BASED ON YOUR NEW CODE

add_action('woocommerce_thankyou', 'create_post_on_order');

function create_post_on_order($order_id)
{
  $order = wc_get_order($order_id);
  if (!$order->get_id()) {
    return;
  }

  $current_user = get_userdata(get_current_user_id());
  $current_user_name = $current_user->display_name;
  $post_title = $order_id . ' - ' . $current_user_name;

  $query = new WP_Query(array(
    "author"    => get_current_user_id(),
    "post_type" => "project",
    "title"     => $post_title
  ));

  if ($query->found_posts == 0) {
    $post_id = wp_insert_post(
      array(
        'post_title'    => $post_title,
        'post_status'   => 'publish',
        'post_type'     => 'project',
        "meta_input"    => array(
          "ponum"       => $order_id
        )
      )
    );

    // It should work without this, this is just a double check!
    if (get_post_type($post_id) == "project") {

      $ponum_key = 'ponum';
      $ponum_field_value = get_field($ponum_key);

      if (empty($ponum_field_value)) {

        $ponum_field_value = $order_id;
        update_field($ponum_key, $ponum_field_value);
      }
    }
  }
}
Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • Hi Ruvee, thank you for your help. I tried it but the order number doesn't go to the field. The ponum fild is empty. I thnk it's because there is no post_id maybe? Sorry to bother you but, do you know how to get the post_id of the user's post on a thank you page? – Aino Apr 03 '21 at 19:04
  • I assumed that you had already created the custom post type. Well, if a customer make a purchase then create his/her custom post type first, then using the function that i wrote above, update the ponum field. – Ruvee Apr 03 '21 at 19:14
  • Hi Ruvee: I created a post (I updated the code) but it still doesn't update the acf field. I tried 'ponum' => $order_id but it doesn’t update the field. I'm a complete beginner.. I'm sure I'm on the wrong way, but I can’t find what’s wrong... Could you please help me? – Aino Apr 03 '21 at 19:39
  • I'll help you, but I have to see your entire code first, so that I could go ahead and debug it for you. you said you have custom post type called "project", right? And inside that custom post type, you have a field called "ponum" right? – Ruvee Apr 03 '21 at 19:46
  • Hi Ruvee: Thank you for your supportive help :). I updated my entire code above. Custom post name is ‘project’, and it has an acf field called ‘ponum’. I need to update the order num to the field ‘ponum’ of the purchased user’s post. I’m not sure but I think I have to use things like global $post, get_field, to get the post_id. Maybe not… – Aino Apr 03 '21 at 20:12
  • @Aino I just updated my answer, please let me know if it works for you! – Ruvee Apr 03 '21 at 21:48
  • WOW, it works perfectly, Thanks million Ruvee, I spent a week to solve this problem, you saved my life. Thank you again for the big gift : ) : ) – Aino Apr 03 '21 at 22:37
  • @Aino No problem, i'm glad i could help. – Ruvee Apr 03 '21 at 22:58
  • Hi Ruvee: I'm sorry but the code creates 2 posts (exactly same posts). I deleted 'double check' part but still creates 2 posts. Would you please help me this? I'm sorry to bother you.. – Aino Apr 03 '21 at 23:00
  • It should NOT! It works fine on my end. Make sure that you delete your previous function that fires on the thankyou hook. Make sure that when you delete your previous function you save the file and then test it again. – Ruvee Apr 03 '21 at 23:12
  • I only had one php and deleted all of them, and tested again, but still creates 2 posts (https://i.imgur.com/k916nhS.jpg). Perviouse code only created one post, I don't know why ... – Aino Apr 03 '21 at 23:22
  • Ok then, I think i know where the problem is. In the ```$query``` replace ```meta_query``` with ```"titles" =>[$post_title]``` – Ruvee Apr 03 '21 at 23:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230714/discussion-between-ruvee-and-aino). – Ruvee Apr 03 '21 at 23:39
  • Hi Ruvee: Wow, perfect, I changed it to "title" =>$post_title, now it creates one with the ponum. Thanks again Ruvee : ) – Aino Apr 04 '21 at 00:18