在Woocommerce 3 +

时间:2018-12-29 14:08:27

标签: php wordpress woocommerce custom-fields hook-woocommerce

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

我们将它们放在一起,但是不久之后我们就停止了正确查看结果的能力。

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

在订单页面中,它出现在两个位置:

1)在“结算信息”下。标签在那里。值是:数组

2)在“自定义字段”部分下。标签在那里。值是:Option_0或Option1等

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

我的第二个问题是,如何才能整体分析这些数据?我们希望能够回答以下问题: 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>';
}

1 个答案:

答案 0 :(得分:0)

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

// 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文件中。经过测试,可以正常工作。

电子邮件通知屏幕截图:

enter image description here