在结帐页面上,我有一个自定义字段,该字段保存到订单元数据中。我需要获取订单元数据,创建自定义帖子类型,并用订单元数据填充自定义字段。
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 );
}
以下函数在签出时创建post,但不拉入上面函数中设置的元字段
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 );
}
我还尝试在保存元顺序数据的函数上设置一个全局变量
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 );
感谢您的反馈。