Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1386891
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 5 years ago.
Improve this questionOn the checkout page I have a custom field that gets saved to the order meta data. I need to grab the order meta data and create a custom post type and fill the custom fields with order meta data.
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
global $mypass;
echo '<p><strong>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'My Field', true ) . '</p>';
$mypass = get_post_meta( $order->id, 'My Field', true );
}
The following functions creates the post on checkout but does not pull in the meta field set in the function above
add_action( 'woocommerce_checkout_update_order_meta', 'create_custom_post' );
function create_custom_post($order, $order_id, $posts) {
global $posts;
$my_post = array(
'post_title' => 'Page Title',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'sale',
);
$website_url = get_post_meta($order_id->id, 'My Field', true);
$new_post_id = wp_insert_post( $my_post );
update_post_meta($new_post_id, 'My Field', $website_url );
}
I have also tried to set a global variable on the function that saves the meta order data
function recent_post_page($order_status, $order_id, $post, $checkout ) {
global $mypass;
$order = new WC_Order( $order_id );
$my_post = array(
'post_title' => 'Page title',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'sale',
);
$new_post_id = wp_insert_post( $my_post );
update_post_meta($new_post_id, 'My Field', $mypass );
}
add_action( 'woocommerce_payment_complete_order_status', 'recent_post_page', 10, 2 );
Any feedback appreciated.
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 5 years ago.
Improve this questionOn the checkout page I have a custom field that gets saved to the order meta data. I need to grab the order meta data and create a custom post type and fill the custom fields with order meta data.
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
global $mypass;
echo '<p><strong>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'My Field', true ) . '</p>';
$mypass = get_post_meta( $order->id, 'My Field', true );
}
The following functions creates the post on checkout but does not pull in the meta field set in the function above
add_action( 'woocommerce_checkout_update_order_meta', 'create_custom_post' );
function create_custom_post($order, $order_id, $posts) {
global $posts;
$my_post = array(
'post_title' => 'Page Title',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'sale',
);
$website_url = get_post_meta($order_id->id, 'My Field', true);
$new_post_id = wp_insert_post( $my_post );
update_post_meta($new_post_id, 'My Field', $website_url );
}
I have also tried to set a global variable on the function that saves the meta order data
function recent_post_page($order_status, $order_id, $post, $checkout ) {
global $mypass;
$order = new WC_Order( $order_id );
$my_post = array(
'post_title' => 'Page title',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'sale',
);
$new_post_id = wp_insert_post( $my_post );
update_post_meta($new_post_id, 'My Field', $mypass );
}
add_action( 'woocommerce_payment_complete_order_status', 'recent_post_page', 10, 2 );
Any feedback appreciated.
Share Improve this question edited Dec 10, 2015 at 16:42 Nwar asked Dec 10, 2015 at 16:08 NwarNwar 311 silver badge4 bronze badges 2 |2 Answers
Reset to default 2I managed to figure it out by placing the wp_insert_post function in the same function that saves the order meta data
Check out $website_url = get_post_meta($order_id->id, 'My Field', true);
You're trying to get the id
of $order_id
.
$order_id
is, or should be a string. You will need to do either:
get_post_meta($order_id, 'My Field', true);
or
get_post_meta($order->ID, 'My Field', true);
本文标签: phpCreate custom post type on successful woocommerce order
版权声明:本文标题:php - Create custom post type on successful woocommerce order 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744572501a2613423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'post_type' => 'page'
you're creating a new Page post type, not a custom post. Have you registered your CPT? – Tim Plummer Commented Dec 10, 2015 at 16:39