Попробуйте следующее, чтобы отобразить значения ваших опубликованных полей в сеансе Woocommerce. Когда заказ будет размещен, он сохранит эти пользовательские данные сеанса как метаданные пользовательского заказа и отобразит их в административных заказах:
// Save the posted data to Woocommerce session
add_filter( 'init', 'set_instagram_posted_data_to_wc_sessions', 10, 3 );
function set_instagram_posted_data_to_wc_sessions() {
if ( ( is_cart() || is_checkout() ) && isset($_POST['igusername']) && isset($_POST['useremail']) ) {
// Enable Woocommerce sessions (if not done yet)
if ( ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
$session_data = []; // initializing
if( isset($_POST['igusername']) && ! empty($_POST['igusername']) ) {
// Add the dropdown value as custom cart item data
$session_data['ig_username'] = sanitize_text_field($_POST['igusername']);
}
if( isset($_POST['useremail']) && ! empty($_POST['useremail']) ) {
// Add the dropdown value as custom cart item data
$session_data['ig_useremail'] = sanitize_email($_POST['useremail']);
}
// Set the data to custom wc_sessions
if( sizeof($session_data) > 0 ) {
WC()->session->set('ig_data', $session_data);
}
}
}
// Save the session data as custom order meta data (post meta data)
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if( $session_data = WC()->session->get('ig_data') ) {
$order->update_meta_data( '_ig_username', wc_clean($session_data['ig_username']) );
$order->update_meta_data( '_ig_useremail', wc_clean($session_data['ig_useremail']) );
// remove the data from Woocommerce session
WC()->session->__unset('ig_data'):
}
}
// Display custom data in Admin orders, below the billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_after_admin_order_billing_address', 10, 1 );
function display_after_admin_order_billing_address( $order ){
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
if( ! empty($ig_username) || ! empty($ig_useremail) ) :
echo '<div class="instagram-userdata">
<h3>'.__('Instagram user data').'</h3>
<table cellpadding="0" cellspacing="0" border="0" style="margin-top:6px;">
<tr><th align="left">'.__('Username').': </th><td> ' . $ig_username . '</td></tr>
<tr><th align="left">'.__('Email').': </th><td> ' . $ig_useremail . '</td></tr>
</table>
</div>';
endif;
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Теперь должно работать.
Если он не работает с сессиями, вы можете использовать следующее, чтобы добавить данные публикации в скрытые поля на странице оформления заказа и снова опубликовать эти данные при отправке заказа ... Все остальное такое же, как указано выше ...
Так что вы можете попробовать поочередно:
// Display the posted data values in checkout hidden fields
add_filter( 'woocommerce_after_checkout_billing_form', 'set_instagram_posted_data_in_hidden_field', 10, 3 );
function set_instagram_posted_data_in_hidden_field() {
if ( isset($_REQUEST['igusername'])|| isset($_REQUEST['useremail']) ) {
// Display hidden fields with the Instagram posted values
?><input type="hidden" name="ig_username" value="<?php echo $_REQUEST['igusername']; ?>">
<input type="hidden" name="ig_useremail" value="<?php echo $_REQUEST['useremail']; ?>"><?php
}
}
// Save checkout hidden fields values as custom order meta data (post meta data)
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if ( isset($_POST['ig_username']) {
$order->update_meta_data( '_ig_username', sanitize_text_field($_POST['ig_username']) );
}
if ( isset($_POST['ig_useremail']) {
$order->update_meta_data( '_ig_useremail', sanitize_email($session_data['ig_useremail']) );
}
}
// Display custom data in Admin orders, below the billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_after_admin_order_billing_address', 10, 1 );
function display_after_admin_order_billing_address( $order ){
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
if( ! empty($ig_username) || ! empty($ig_useremail) ) :
echo '<div class="instagram-userdata">
<h3>'.__('Instagram user data').'</h3>
<table cellpadding="0" cellspacing="0" border="0" style="margin-top:6px;">
<tr><th align="left">'.__('Username').': </th><td> ' . $ig_username . '</td></tr>
<tr><th align="left">'.__('Email').': </th><td> ' . $ig_useremail . '</td></tr>
</table>
</div>';
endif;
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Это должно работать сейчас.
На страницах заказа в бэкэнде вы получите что-то вроде:

Для извлечения данных из $order
объекта WC_Order
(или $order_id
Идентификатор заказа) используйте:
$order = wc_get_order( $order_id ); // (optionally if required) with the Order ID
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');