如何从客户电子邮件和订单中的 select 字段获取 order_meta 详细信息?

How to get order_meta details from a select field in customer email and orders?

很高兴知道: 我根据产品的数量创建了额外的字段。因此,如果有人两次购买该产品,他必须在结账时填写两个字段。如果有人购买产品 1x,他们必须填写一次该字段。

这些额外字段包含“select 选项”。我需要在我的订单概览和来自 woocommerce 的电子邮件中获取元数据。

//Custom WooCommerce Checkout Fields based on Quantity
add_action( 'woocommerce_before_order_notes', 'person_details' );

function person_details($checkout) {
    global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 0;
       for($k=1; $k<= $count; $k++) {
        $i++;
           print ('Gegevens van kind '.$i.'');
            woocommerce_form_field( 'cstm_full_name'.$i, array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Volledige naam'),
            'placeholder'   => __(''),
            'required' => true,
            ),
            $checkout->get_value( 'cstm_full_name'.$i ));
        echo '<div class="clear"></div>';
        woocommerce_form_field( 'cstm_gender', array(
            'type'          => 'select',
            'required' => true,
            'class'         => array( 'my-field-class form-row-wide' ),
            'label'         => __( 'Jongen, Meisje of Genderneutraal' ),
            'options'       => array(
                 'blank'             => __( 'Selecteer', 'njengah' ),
                 'jongen'           => __( 'Jongen', 'njengah' ),
                 'meisje'               => __( 'Meisje', 'njengah' ),
                 'genderneutraal'   => __( 'Genderneutraal', 'njengah' )
             )),
            $checkout->get_value( 'cstm_gender' ));
        woocommerce_form_field( 'cstm_leeftijd'.$i, array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-first'),
            'label'         => __('Leeftijd bij aanvang kamp'),
            'placeholder'   => __(''),
            'required' => true,
            ),
            $checkout->get_value( 'cstm_leeftijd'.$i ));
        woocommerce_form_field( 'cstm_groep'.$i, array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-last'),
            'label'         => __('In welke huidige Groep'),
            'placeholder'   => __(''),
            'required' => true,
            ),
            $checkout->get_value( 'cstm_groep'.$i ));
        woocommerce_form_field( 'cstm_shirt', array(
            'type'          => 'select',
            'required' => true,
            'class'         => array( 'my-field-class form-row-first' ),
            'label'         => __( 'Shirt size' ),
            'options'       => array(
                 'blank'             => __( 'Selecteer', 'njengah' ),
                 'Maat S'           => __( 'S', 'njengah' ),
                 'Maat M'               => __( 'M', 'njengah' ),
                 'Maat L'   => __( 'L', 'njengah' )
             )),
            $checkout->get_value( 'cstm_shirt' ));
        woocommerce_form_field( 'cstm_ervaring', array(
            'type'          => 'select',
            'required' => true,
            'class'         => array( 'my-field-class form-row-last' ),
            'label'         => __( 'Summer Camp experience' ),
            'options'       => array(
                'blank'                     => __( 'Selecteer', 'njengah' ),
                '1e keer Summer Camp'       => __( '1e keer Summer Camp', 'njengah' ),
                '2e keer Summer Camp'       => __( '2e keer Summer Camp', 'njengah' ),
                '3e keer Summer Camp'       => __( '3e keer Summer Camp', 'njengah' ),
                '4e keer Summer Camp'       => __( '4e keer Summer Camp', 'njengah' )
             )),
            $checkout->get_value( 'cstm_ervaring' ));
           echo '<div class="clearbox"></div>';
}
}




/**
 * Save value of fields
 */

add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');

function customise_checkout_field_update_order_meta($order_id)
{
    global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 0;
       for($k=1; $k<= $count; $k++) {
        $i++;
    if (!empty($_POST['cstm_full_name'.$i])) {
        update_post_meta($order_id, 'Naam van kind'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]));
    }
    if (!empty($_POST['cstm_gender'.$i])) {
        update_post_meta($order_id, 'Jongen, Meisje of Genderneutraal'.$i, sanitize_text_field($_POST['cstm_gender'.$i]));
    }
    if (!empty($_POST['cstm_leeftijd'.$i])) {
        update_post_meta($order_id, 'Leeftijd bij aanvang kamp'.$i, sanitize_text_field($_POST['cstm_leeftijd'.$i]));
    }
    if (!empty($_POST['cstm_groep'.$i])) {
        update_post_meta($order_id, 'In welke huidige Groep'.$i, sanitize_text_field($_POST['cstm_groep'.$i]));
    }
    if ( isset( $_POST['cstm_groep'.$i] )){
        $value = sanitize_text_field( $_POST['cstm_groep'.$i] );
        update_post_meta( $post->ID, 'cstm_groep'.$i, $value );  
    }
}
}


/**
 * Add fields to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$i = 0;
for($k=1; $k<= 50; $k++) {
$i++;
$keys[] = 'Naam van kind'.$i;
$keys[] = 'Jongen, Meisje of Genderneutraal'.$i;
$keys[] = 'Leeftijd bij aanvang kamp'.$i;
$keys[] = 'In welke huidige Groep'.$i;
$keys[] = '_cstm_gender'.$i;
$keys[] = '_cstm_shirt'.$i;
$keys[] = '_cstm_ervaring'.$i;
}   
return $keys;
}

您可以使用 woocommerce_email_order_meta 操作挂钩将自定义元数据添加到电子邮件中,并使用 woocommerce_admin_order_data_after_order_details 挂钩在概览页面上显示自定义元数据。试试下面的代码。

function person_details( $checkout ) {

    global $woocommerce;
    $count = $woocommerce->cart->cart_contents_count;
    $i = 0;

    for( $k=1; $k<= $count; $k++ ) {
        $i++;
        print ('Gegevens van kind '.$i.'');
        
        woocommerce_form_field( 'cstm_full_name'.$i, array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Volledige naam'),
            'placeholder'   => __(''),
            'required' => true,
        ), $checkout->get_value( 'cstm_full_name'.$i ));
        
        echo '<div class="clear"></div>';
        
            woocommerce_form_field( 'cstm_gender'.$i, array(
                'type'          => 'select',
                'required' => true,
                'class'         => array( 'my-field-class form-row-wide' ),
                'label'         => __( 'Jongen, Meisje of Genderneutraal' ),
                'options'       => array(
                     'blank'             => __( 'Selecteer', 'njengah' ),
                     'jongen'           => __( 'Jongen', 'njengah' ),
                     'meisje'               => __( 'Meisje', 'njengah' ),
                     'genderneutraal'   => __( 'Genderneutraal', 'njengah' )
            )), $checkout->get_value( 'cstm_gender'.$i ));
            
            woocommerce_form_field( 'cstm_leeftijd'.$i, array(
                'type'          => 'text',
                'class'         => array('my-field-class form-row-first'),
                'label'         => __('Leeftijd bij aanvang kamp'),
                'placeholder'   => __(''),
                'required' => true,
            ), $checkout->get_value( 'cstm_leeftijd'.$i ));
            
            woocommerce_form_field( 'cstm_groep'.$i, array(
                'type'          => 'text',
                'class'         => array('my-field-class form-row-last'),
                'label'         => __('In welke huidige Groep'),
                'placeholder'   => __(''),
                'required' => true,
            ), $checkout->get_value( 'cstm_groep'.$i ));
            
            woocommerce_form_field( 'cstm_shirt', array(
                'type'          => 'select',
                'required' => true,
                'class'         => array( 'my-field-class form-row-first' ),
                'label'         => __( 'Shirt size' ),
                'options'       => array(
                     'blank'             => __( 'Selecteer', 'njengah' ),
                     'Maat S'           => __( 'S', 'njengah' ),
                     'Maat M'               => __( 'M', 'njengah' ),
                     'Maat L'   => __( 'L', 'njengah' )
            )), $checkout->get_value( 'cstm_shirt' ));
            
            woocommerce_form_field( 'cstm_ervaring', array(
                'type'          => 'select',
                'required' => true,
                'class'         => array( 'my-field-class form-row-last' ),
                'label'         => __( 'Summer Camp experience' ),
                'options'       => array(
                    'blank'                     => __( 'Selecteer', 'njengah' ),
                    '1e keer Summer Camp'       => __( '1e keer Summer Camp', 'njengah' ),
                    '2e keer Summer Camp'       => __( '2e keer Summer Camp', 'njengah' ),
                    '3e keer Summer Camp'       => __( '3e keer Summer Camp', 'njengah' ),
                    '4e keer Summer Camp'       => __( '4e keer Summer Camp', 'njengah' )
            )), $checkout->get_value( 'cstm_ervaring' ));
        
        echo '<div class="clearbox"></div>';
    }
}
add_action( 'woocommerce_before_order_notes', 'person_details' );

function customise_checkout_field_update_order_meta($order_id)
{
    global $woocommerce;
    $count = $woocommerce->cart->cart_contents_count;
    $i = 0;
    for($k=1; $k<= $count; $k++) {
        $i++;
        if (!empty($_POST['cstm_full_name'.$i])) {
            update_post_meta($order_id, 'Naam van kind'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]));
        }
        if (!empty($_POST['cstm_gender'.$i])) {
            update_post_meta($order_id, 'Jongen, Meisje of Genderneutraal'.$i, sanitize_text_field($_POST['cstm_gender'.$i]));
        }
        if (!empty($_POST['cstm_leeftijd'.$i])) {
            update_post_meta($order_id, 'Leeftijd bij aanvang kamp'.$i, sanitize_text_field($_POST['cstm_leeftijd'.$i]));
        }
        if (!empty($_POST['cstm_groep'.$i])) {
            update_post_meta($order_id, 'In welke huidige Groep'.$i, sanitize_text_field($_POST['cstm_groep'.$i]));
        }
        if ( isset( $_POST['cstm_groep'.$i] )){
            $value = sanitize_text_field( $_POST['cstm_groep'.$i] );
            update_post_meta( $post->ID, 'cstm_groep'.$i, $value );  
        }
    }
}
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');

function add_email_custom_order_meta( $order, $sent_to_admin, $plain_text ){

    $quantity = 0;      
    foreach ( $order->get_items() as $item_id => $item ) {
        $quantity = $quantity + $item->get_quantity();
    }
    
    $order_id = $order->get_order_number();
    echo "<ul>";
        $i = 0;
        for( $k=1; $k <= $quantity; $k++ ) {
            $i++;
            echo "<li>Volledige naam: ".get_post_meta($order_id, 'Naam van kind'.$i, true )."</li>";
            echo "<li>Jongen, Meisje of Genderneutraal: ".get_post_meta($order_id, 'Jongen, Meisje of Genderneutraal'.$i, true )."</li>";
            echo "<li>Leeftijd bij aanvang kamp: ".get_post_meta($order_id, 'Leeftijd bij aanvang kamp'.$i, true )."</li>";
            echo "<li>In welke huidige Groep: ".get_post_meta($order_id, 'In welke huidige Groep'.$i, true )."</li>";
            echo "<li>In welke huidige Groep: ".get_post_meta( $post->ID, 'cstm_groep'.$i, true )."</li>";
            
        }
    echo "</ul>";

}
add_action( 'woocommerce_email_order_meta', 'add_email_custom_order_meta', 10, 3 );

// display the extra data in the order admin panel
function display_order_custom_data_in_admin_order_overview_page( $order ){  ?>
    <div class="order_data_column" style="width: 100% !important;">
        <h4><?php _e( 'Your label' ); ?></h4>
        <?php 
            $quantity = 0;      
            foreach ( $order->get_items() as $item_id => $item ) {
                $quantity = $quantity + $item->get_quantity();
            }
            
            $order_id = $order->get_order_number();
            echo "<ul>";
                $i = 0;
                for( $k=1; $k <= $quantity; $k++ ) {
                    $i++;
                    echo "<li>Volledige naam: ".get_post_meta($order_id, 'Naam van kind'.$i, true )."</li>";
                    echo "<li>Jongen, Meisje of Genderneutraal: ".get_post_meta($order_id, 'Jongen, Meisje of Genderneutraal'.$i, true )."</li>";
                    echo "<li>Leeftijd bij aanvang kamp: ".get_post_meta($order_id, 'Leeftijd bij aanvang kamp'.$i, true )."</li>";
                    echo "<li>In welke huidige Groep: ".get_post_meta($order_id, 'In welke huidige Groep'.$i, true )."</li>";
                    echo "<li>In welke huidige Groep: ".get_post_meta( $post->ID, 'cstm_groep'.$i, true )."</li>";
                    
                }
            echo "</ul>";    
        ?>
    </div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_custom_data_in_admin_order_overview_page' );

已测试并有效。

订单概览页面

电子邮件