在WooCommerce收到的订单页面和电子邮件上显示订单自定义字段

时间:2019-05-03 15:49:48

标签: php wordpress woocommerce orders email-notifications

我有基于购物车项目数量的自定义结帐字段。现在,我需要在“已收到订单”页面和电子邮件通知上显示已保存的订单元数据。

这是字段的代码: 根据购物车数量添加自定义字段

add_action( 'woocommerce_before_checkout_billing_form', 'srd_custom_einstiegswahl');
function srd_custom_einstiegswahl( $checkout ){

    $count = 1;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        $options       = array( '' => __("Bitte wählen Sie Ihren Einstiegsort") ); 
        $einstiegsorte = get_post_meta( $cart_item[ 'product_id' ], '_einstiegsorte', true );

        if( ! empty($einstiegsorte) ){
            $option_items = explode( "\n", $einstiegsorte );
            if( sizeof( $option_items ) > 1 ){
                foreach( $option_items as $value )
                    $options[$value] = $value;
            } else {
                $value = str_replace('\n', '', $einstiegsorte);
                $options[$value] = $value;
            }
        }

        // Loop through cart item quantity
        for($i = 1; $i <= $cart_item['quantity']; $i++ ) {

            $j = $count.'_'.$i;

            echo '<h6>Reiseteilnehmer '.$i . '</h6>';

            woocommerce_form_field( '_teilnehmer_vorame_'.$j, array(
                    'type'          => 'text',
                     'class'         => array('checkout_vorname'),
                    'label'         => __('Vorame'),
                    'required'      => true,
            ), $checkout->get_value( '_teilnehmer_vorame_'.$j ));

            woocommerce_form_field( '_teilnehmer_nachname_'.$j, array(
                    'type'          => 'text',
                    'class'         => array('checkout_nachname'),
                    'label'         => __('Nachname'),
                    'required'      => true,
            ), $checkout->get_value( '_teilnehmer_nachname_'.$j ));

            woocommerce_form_field( '_teilnehmer_einstiegswahl_'.$j, array(
                'type'          => 'select',
                'class'         => array('checkout_einstiegswahl'),
                'label'         => __('Einstiegswahl'),
                'required'      => true,
                'options'       => $options,
            ), $checkout->get_value( '_teilnehmer_einstiegswahl_'.$j ));
        }
        $count++;
    }
}

使用结帐自定义字段值更新订单元数据

add_action('woocommerce_checkout_create_order', 'srd_teilnehmer_checkout_create_order', 20, 2 );
function srd_teilnehmer_checkout_create_order( $order, $data ) {
    $count = 1;

    // Loop through cart item quantity
    foreach( WC()->cart->get_cart() as $cart_item ) {

        // Loop through item quantity
        for($i = 1; $i <= $cart_item['quantity']; $i++ ) {

            $j = $count.'_'.$i;

            if ( isset($_POST['_teilnehmer_vorame_'.$j]) )
                $order->update_meta_data( '_teilnehmer_vorame_'.$j , sanitize_text_field($_POST['_teilnehmer_vorame_'.$j]) );

            if ( isset($_POST['_teilnehmer_nachname_'.$j]) )
                $order->update_meta_data( '_teilnehmer_nachname_'.$j, sanitize_text_field($_POST['_teilnehmer_nachname_'.$j]) );

            if ( isset($_POST['_teilnehmer_einstiegswahl_'.$j]) )
                $order->update_meta_data( '_teilnehmer_einstiegswahl_'.$j, sanitize_text_field($_POST['_teilnehmer_einstiegswahl_'.$j]) );
        }
        $count++;
    }
}

这是我尝试显示已保存的字段:

function srdreiseteilnehmer($order_id) {

    $count = 1;

    $key_labels = array( 'vorame', 'nachname', 'einstiegswahl' );

    // Loop through order items
    foreach ( $order_id->get_items() as $item ){



        // Loop through item quantity
        for($i = 1; $i <= $item->get_quantity(); $i++ ) {

            echo '<tbody>';



            // Loop through attendee fields
            foreach( $key_labels as $key ){
                $value = get_post_meta( $order_id, '_teilnehmer_'.$key.'_'.$count.'_'.$i, true );
                echo '<tr><th>'.ucfirst($key).':</th><td>'.$value.'</td></tr>';
            }

            echo '</tbody>';
        }
        $count++;
    }
}

我计划将此功能挂接到我要显示数据的所有位置,但是不起作用...

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我已经重新访问了您的代码(这似乎是基于我先前的回答之一)

我为结帐页面添加了缺少的字段验证,在“收到的订单(谢谢)”页面和电子邮件通知上显示了参与者数据:

// Participant fields labels names
function teilnehmer_fields_labels(){
    $domain = 'woocommerce';
    return [
        __('Vorame', $domain),
        __('Nachname', $domain),
        __('Einstiegswahl', $domain)
    ];
}

// Checkout custom fields display
add_action( 'woocommerce_before_checkout_billing_form', 'srd_custom_einstiegswahl');
function srd_custom_einstiegswahl( $checkout ){
    $teilnehmer_labels = teilnehmer_fields_labels();
    $count = 1;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {

        // Loop through cart item quantity
        for($i = 1; $i <= $cart_item['quantity']; $i++ ) {
            echo '<h6>Reiseteilnehmer '.$i . '</h6>';

            // Loop through participants keys / labels pairs
            foreach( $teilnehmer_labels as $label ){
                $key = strtolower($label);
                $field_key = '_teilnehmer_'.$key.'_'.$count.'_'.$i;

                woocommerce_form_field( $field_key, array(
                        'type'          => 'text',
                         'class'         => array('form-row-wide checkout_'.$key),
                        'label'         => $label,
                        'required'      => true,
                ), '' );
            }
        }
        $count++;
    }
}

// Checkout custom fields save to order meta
add_action('woocommerce_checkout_create_order', 'srd_teilnehmer_checkout_create_order', 10, 2 );
function srd_teilnehmer_checkout_create_order( $order, $data ) {
    $teilnehmer_labels = teilnehmer_fields_labels();
    $count = 1;

    // Loop through cart item quantity
    foreach( $order->get_items() as $item ) {

        // Loop through item quantity
        for($i = 1; $i <= $item->get_quantity(); $i++ ) {

            // Loop through participants keys / labels pairs
            foreach( $teilnehmer_labels as $label ) {
                $field_key = '_teilnehmer_'.strtolower($label).'_'.$count.'_'.$i;

                if ( isset($_POST[$field_key]) ) {
                    $order->update_meta_data( $field_key , sanitize_text_field($_POST[$field_key]) );
                }
            }
        }
        $count++;
    }
}

// Checkout custom fields validation
add_action('woocommerce_checkout_process', 'srd_teilnehmer_checkout_process' );
function srd_teilnehmer_checkout_process() {
    $teilnehmer_labels = teilnehmer_fields_labels();
    $count = 1;
    $error = false;

    // Loop through cart item quantity
    foreach( WC()->cart->get_cart() as $cart_item ) {

        // Loop through item quantity
        for($i = 1; $i <= $cart_item['quantity']; $i++ ) {

            // Loop through participants keys / labels pairs
            foreach( $teilnehmer_labels as $label ) {
                $field_key = '_teilnehmer_'.strtolower($label).'_'.$count.'_'.$i;

                if ( isset($_POST[$field_key]) && empty($_POST[$field_key]) ) {
                    $error = true;
                }
            }
        }
        $count++;
    }

    if ( $error ) {
        wc_add_notice( __( 'Please fill in all participants fields' ), 'error' );
    }
}

// Display order custom meta data in Order received (thankyou) page
add_action('woocommerce_thankyou', 'srd_teilnehmer_thankyou', 10, 2 );
function srd_teilnehmer_thankyou( $order_id ) {
    $teilnehmer_labels = teilnehmer_fields_labels();
    $order = wc_get_order( $order_id );
    $count = 1;

    // Loop through order items
    foreach ( $order->get_items() as $item ){

        // Loop through item quantity
        for($i = 1; $i <= $item->get_quantity(); $i++ ) {
            echo '<h6>Reiseteilnehmer '.$i . '</h6>';
            echo '<table><tbody>';

            // Loop through participants keys / labels pairs
            foreach( $teilnehmer_labels as $label ){
                $meta_key = '_teilnehmer_'.strtolower($label).'_'.$count.'_'.$i;

                echo '<tr><th>'.$label.':</th><td>'.$order->get_meta( $meta_key ).'</td></tr>';
            }

            echo '</tbody></table>';
        }
        $count++;
    }
}

// Display order custom meta data on email notifications
add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
function action_after_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    $teilnehmer_labels = teilnehmer_fields_labels();
    $count = 1;

    // The HTML Structure
    $html_output = '<h2>' . __( 'Teilnehmer', 'woocommerce' ) . '</h2>
    <div class="teilnehmer-info">
        <table cellspacing="0" cellpadding="6"><tbody>';

    // Loop through order items
    foreach ( $order->get_items() as $item ){

        // Loop through item quantity
        for($i = 1; $i <= $item->get_quantity(); $i++ ) {
            $html_output .= '<tr class="subtitle"><td colspan="2"><h3>' . __("Reiseteilnehmer", "woocommerce") . $i . '</h3></td></tr>';


            // Loop through participants keys / labels pairs
            foreach( $teilnehmer_labels as $label ){
                $meta_key = '_teilnehmer_'.strtolower($label).'_'.$count.'_'.$i;

                $html_output .= '<tr><th>'.$label.':</th><td>'.$order->get_meta( $meta_key ).'</td></tr>';
            }
        }
        $count++;
    }

    $html_output .= '</tbody></table>
    </div><br>'; // HTML (end)

    // The CSS styling
    $styles = '<style>
        .teilnehmer-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
            color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
        .teilnehmer-info table th, table.teilnehmer-info td{text-align: left; border-top-width: 4px;
            color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
        .teilnehmer-info table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
        .teilnehmer-info table tr.subtitle h3{margin: 0;}
    </style>';

    // The Output CSS + HTML
    echo $styles . $html_output;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

在收到订单页面的末尾:

enter image description here

关于电子邮件通知:

enter image description here