1

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.

  • 'post_type' => 'page' you're creating a new Page post type, not a custom post. Have you registered your CPT? – Tim Plummer Dec 10 '15 at 16:39
  • I made a typo there, my main focus is to get the custom fields from order details meta data – Nwar Dec 10 '15 at 16:40
1

I managed to figure it out by placing the wp_insert_post function in the same function that saves the order meta data

0

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);
  • Same issue, the value is empty for the field. – Nwar Dec 10 '15 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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