在Woocommerce 3中保存并显示订单自定义元数据 [英] Save and display order custom meta data in Woocommerce 3+

查看:72
本文介绍了在Woocommerce 3中保存并显示订单自定义元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我的搭档帮助我在我们的结帐页面中添加了一个自定义字段,我们想问人们您是怎么知道我们的?他们预定我们的活动时。

A while back my partner helped me to add a custom field to our checkout page, we wanted to ask people "How did you hear about us?" when they booked for our events.

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

We put it together but shortly after we stopped being able to see the results properly.

在发送给管理员的新订单电子邮件中,标签您是如何得知我们的出现了,但没有答案。

In the "New Order" emails sent to the admin, the label 'How did you hear about us' appears, but with no answer.

在订单页面中,它显示在两个位置:

In the order page, it appears in two places:

1)在结算信息下。标签在那里。值是:自定义字段部分下的数组

1) under the Billing info. The label is there. Value is: Array

2)。标签在那里。值是:Option_0或Option1等

2) under "Custom Fields" section. The label is there. Value is: Option_0 or Option1 etc

我的第一个问题是如何解决此问题,例如社交媒体出现在电子邮件和订单页面上。

My first question is how can I resolve this so that the option, e.g. "Social Media" appears on the email and order pages.

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

My second question, is, how can I analyse this data as a whole? We would hopefully like to be able to answer questions like; how many people chose option 1 in 2018?; how many chose option 2 in December?; how many purchased x who chose option 3?; how many purchased y and chose option 4?

在此先感谢您的帮助!

Thank you in advance for your help!

/**
* 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>';
}


推荐答案

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

There are some mistakes and errors in your code… Try the following revisited code:

// 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文件中主题(或活动主题)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

电子邮件通知屏幕截图:

Email Notifications screenshot:

这篇关于在Woocommerce 3中保存并显示订单自定义元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆