0

Working on a custom function I would like to create post everytime order is created in woocommerce. I need woocommerce order id in the post title And list of items in the post content. Anyone made something similar? I did it with zapier so far but decided it would be much more efficient if everyhing happens on the server.

1
  • WooCommerce and other 3rd party plugin/theme dev support is off topic and not in this stacks scope. You should ask via their official support routes or in their groups and communities.
    – Tom J Nowell
    Oct 11 at 10:56
0

You could hook a custom post creation function to the woocommerce_thankyou action that is fired on the thank you page after the order has been created. This action gives you the $order_id as a parameter.

The post creation function could be something like this,

function create_post_after_order( $order_id ) {
  if (  $order_id ) {
    return;
  }      
  $order = wc_get_order( $order_id );
  $order_items = $order->get_items();    
  $content = '<ul>';
  // I'm not entirely sure of this, please the check that the methods are correct
  foreach ( $order_items as $item_id => $item_data ) {
    $product = $item_data->get_product();
    $content .= '<li>' . $product->get_name() . '</li>';
  }
  $content .= '</ul>';    
  $postarr = array(
    'post_title'   => "Order {$order_id}",
    'post_content' => $content // I'm not sure if you need to turn the tags into html entities or if wp_insert_post accepts the content as is
    // add more args as needed
  );    
  $new_post_id = wp_insert_post( $postarr );  
}
add_action( 'woocommerce_thankyou', 'create_post_after_order' );
0

Thank you so much for this! it gave me some ideas how to achieve it i had to change thank you action to payment complete as it didnt created posts for POS orders or manual orders. also ( $postarr ); doesnt seem to work

Here is the working 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' => 'custom',
  );    
  $post_id = wp_insert_post($new_post);
}
add_action( 'woocommerce_payment_complete' , 'create_post_after_order' );
0

Just Using woocommerce_payment_complete hook Try It !

function create_custom_post_after_order( $order_id ) {
  if (  $order_id ) {
    $post = array();
    $post['post_type'] = 'post';
    $post['post_title'] = 'Custom Post';
    $post['post_content'] = 'Custom Post'.$order_id;
    $post['post_status'] = 'Publish';

    wp_insert_post($post);
  }      
 
}

add_action( 'woocommerce_payment_complete' , 'create_custom_post_after_order' );

Not the answer you're looking for? Browse other questions tagged or ask your own question.