Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

wordpress - Creating a new post type on order completion and populating ACF fields from order info

This is my first question. Thanks for your input!

I'm trying to create a new custom post upon completion of a WooCommerce order (which I have managed.. thanks to another answer on the brilliant stackoverflow!)

I'm using this code:

    function create_post_after_order( $order_id ) {
  if (  $order_id instanceof WC_Order ){
    return;
  }      
  $order = wc_get_order( $order_id );
  $order_items = $order->get_items();    
  $content = '<ul>';
  foreach ( $order_items as $item_id => $item_data ) {
    $product = $item_data->get_product();
    $content .= '<li>' . $product->get_name() . '</li>';
  }
  $content .= '</ul>';    
  $new_post = array(
    'post_title'   => "Order {$order_id}",
    'post_content' => $content,
    'post_status' => 'private',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'ecard',
    'post_status'   => 'publish'
  );    
  $post_id = wp_insert_post($new_post);
}
add_action( 'woocommerce_thankyou', 'create_post_after_order', 10, 1 );

I have collected information from the user on the product page using Woocommerce Custom Product Addons.

Upon the creation of this new Card post I want to populate various ACF fields with the information acquired by Woocommerce Custom Product Addons.

My ACF fields are:

  • ACF_recipient_name (field ID in WPCA is wcpa-text-1609894989537) - or do i need to set a class/name?
  • ACF_recipient_description (field ID in WPCA is wcpa-text-1609895848519)List item
  • ACF_card_message (field ID in WPCA is wcpa-textarea-1609895020087)
  • ACF_background_colour (field ID in WPCA is wcpa-color-1609895616511)
  • ACF_delivery_date (field ID in WPCA is wcpa-date-1609895171936)

Do you have any suggestions as to how I can retrieve the WPCA data from the order and pass it over to the ACF fields in the new post type?

Any suggestions appreciated!

many thanks

question from:https://stackoverflow.com/questions/65601348/creating-a-new-post-type-on-order-completion-and-populating-acf-fields-from-orde

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...