Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1287125
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionWorking 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.
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionWorking 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.
Share Improve this question edited Mar 5, 2019 at 10:57 Jacob Peattie 44.1k10 gold badges50 silver badges64 bronze badges asked Mar 5, 2019 at 8:34 CSUKCSUK 34 bronze badges 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 ♦ Commented Oct 11, 2021 at 10:56
3 Answers
Reset to default 0You 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' );
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' );
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' );
本文标签: Create post when new woocommerce order is created
版权声明:本文标题:Create post when new woocommerce order is created 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741277601a2369820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论