admin管理员组

文章数量:1386891

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 question

On 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 question

On 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
  • '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
  • I made a typo there, my main focus is to get the custom fields from order details meta data – Nwar Commented Dec 10, 2015 at 16:40
Add a comment  | 

2 Answers 2

Reset to default 2

I 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