在 Woocommerce 3+ 中保存和显示订单自定义元数据

Save and display order custom meta data in Woocommerce 3+

不久前,我的合作伙伴帮助我在结帐页面添加了一个自定义字段,我们想询问人们 "How did you hear about us?" 何时预订了我们的活动。

我们把它放在一起,但不久之后我们就无法正确看到结果了。

在发送给管理员的 "New Order" 封电子邮件中,出现了标签 'How did you hear about us',但没有回复。

在订单页面,它出现在两个地方:

1) 在帐单信息下。标签在那里。值为:数组

2) 在 "Custom Fields" 部分下。标签在那里。值为:Option_0 或 Option1 等

我的第一个问题是如何解决这个问题,以便选项,例如"Social Media" 出现在电子邮件和订单页面上。

我的第二个问题是,如何从整体上分析这些数据?我们希望能够回答以下问题; 2018 年有多少人选择了选项 1?有多少人在 12 月选择了选项 2?有多少人购买了 x 谁选择了选项 3?;有多少人购买了 y 并选择了选项 4?

提前感谢您的帮助!

/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

echo '<div id="my_custom_checkout_field"><h2>' . __('Please help us understand our customers so that we can improve future events (Optional)') . '</h2>';

woocommerce_form_field( 'hearaboutus', array(
    'type'          => 'select',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('How did you hear about us? &nbsp;'),
    'options'       => array(
 'Option_0' => 'Please select...',
 'option_1' => 'Social Media (e.g Facebook)',
 'option_2' => 'Search Engine (e.g Google)',
 'option_3' => 'Meditation Class',
 'option_4' => 'Leaflets/Flyers/Posters',
 'option_5' => 'Website',
 'option_6' => 'Email Newsletter',
 'option_7' => 'Other',
 )
    ), $checkout->get_value( 'hearaboutus' ));

echo '</div>';

}

/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['hearaboutus'] ) ) {
    update_post_meta( $order_id, 'How did you hear about us?', 
sanitize_text_field( $_POST['hearaboutus'] ) );
}
}

/* Add the fields to order email */

add_filter( 'woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys' );
function my_custom_checkout_field_order_meta_keys( $keys ) {
            echo '<h3>How did you hear about us?:</h3>';
    $keys[''] = 'hearaboutus';
    return $keys;
}

/**
* Display field value on the order edit page
*/
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){
echo '<p><strong>'.__('How did you hear about us?').':</strong> ' . get_post_meta( $order->id, $key='', 'hearaboutus', true ) . '</p>';
}

您的代码中存在一些错误和错误......请尝试以下重新访问的代码:

// get "hearaboutus" select field options data
function wc_get_hearaboutus_options(){
    return array(
        ''          => 'Please select...',
        'option_1'  => 'Social Media (e.g Facebook)',
        'option_2'  => 'Search Engine (e.g Google)',
        'option_3'  => 'Meditation Class',
        'option_4'  => 'Leaflets/Flyers/Posters',
        'option_5'  => 'Website',
        'option_6'  => 'Email Newsletter',
        'option_7'  => 'Other',
    );
}

// Add the field to the checkout
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h3>' . __('Please help us understand our customers so that we can improve future events (Optional)') . '</h3>';

    woocommerce_form_field( '_hearaboutus', array(
        'type'    => 'select',
        'class'   => array('my-field-class form-row-wide'),
        'label'   => __('How did you hear about us? &nbsp;'),
        'options' => wc_get_hearaboutus_options(),
    ), $checkout->get_value( '_hearaboutus' ) );

    echo '</div>';

}

// Update the order meta with field value
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_create_order', 10, 2 );
function custom_checkout_field_create_order( $order, $data ) {
    if ( isset($_POST['_hearaboutus']) && ! empty($_POST['_hearaboutus']) ) {
         $order->update_meta_data( '_hearaboutus', sanitize_text_field($_POST['_hearaboutus']) );
    }
}

// Add the fields to order email
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 ) {
    if( $hearaboutus = $order->get_meta('_hearaboutus') ) {
        // The data
        $label = __('How did you hear about us?');
        $value = wc_get_hearaboutus_options()[$hearaboutus];

        // The HTML Structure
        $html_output = '<h2>' . __('Extra data') . '</h2>
        <div class="discount-info"><table cellspacing="0" cellpadding="6"><tr>
        <th>' . $label . '</th><td>' . $value . '</td>
        </tr></tbody></table></div><br>';

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

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

// Display field value on the order edit page
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 ) {
    if( $hearaboutus = $order->get_meta('_hearaboutus') ) {
        $value = wc_get_hearaboutus_options()[$hearaboutus];
        echo '<p><strong>'.__('How did you hear about us?').'</strong> ' . $value . '</p>';
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

电子邮件通知屏幕截图: