在订单电子邮件和订单编辑页面中显示复选框字段数据

时间:2019-02-25 18:47:11

标签: php wordpress woocommerce custom-fields orders

一个月前Save and display order custom meta data in Woocommerce 3+答案代码帮助我保存并显示了自定义结帐字段(“您如何得知我们的消息”字段下拉列表)转到订单电子邮件和订单编辑页面。

现在,我需要添加另一个复选框以询问用户是否要注册WhatsApp列表。

我使用some code创建了这个新的结帐字段,尝试了几种不同的方法。但是我似乎无法使其以与上述相同的方式出现在订单编辑页面和电子邮件中! (不显示“数组”或根本不显示...)

这是代码:

// Add the WhatsApp Checkbox
function cw_custom_checkbox_fields( $checkout ) {
echo '<div class="cw_custom_class"><h2>'.__('Would you like to be added to the WhatsApp list?').'</h2>';
woocommerce_form_field( 'custom_checkbox', array(
    'type'          => 'checkbox',
    'label'         => __('Yes, add me to the list!'),
    'required'  => false,
), $checkout->get_value( 'custom_checkbox' ));
echo '</div>';
}

add_action('woocommerce_after_order_notes', 'cw_custom_checkbox_fields');

// Save the WhatsApp Data
add_action('woocommerce_checkout_update_order_meta', 'cw_checkout_order_meta');
function cw_checkout_order_meta( $order_id ) {
if ($_POST['custom_checkbox']) update_post_meta( $order_id, 'checkbox name', esc_attr($_POST['custom_checkbox']));
}

非常感谢您在电子邮件和订单编辑页面中显示标签和价值的帮助!

1 个答案:

答案 0 :(得分:2)

这两个字段在命令管理和电子邮件通知中合并,保存和显示:

// 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 fields 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>';

    echo '<div class="cw_custom_class">
    <h2>'.__('Would you like to be added to the WhatsApp list?').'</h2>';

    $whatsapp_list =

    woocommerce_form_field( 'whatsapp_list', array(
        'type'          => 'checkbox',
        'label'         => __("Yes, add me to the list!", "woocommerce"),
        'required'  => false,
    ), $checkout->get_value( 'whatsapp_list' ) );

    echo '</div>';
}

// Update the order meta with fields values
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']) );
    }
    if ( isset($_POST['whatsapp_list']) ) {
        $order->update_meta_data( '_whatsapp_list', 'Yes' );
    }
}

// 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 1
        $label1 = __('How did you hear about us?');
        $value1 = wc_get_hearaboutus_options()[$hearaboutus];
    }

    if( $whatsapp_list = $order->get_meta('_whatsapp_list') ) {
        // The data 1
        $label2 = __('Add me to WhatsApp list?');
        $value2 = $whatsapp_list;
    }

    if( isset($value1) && isset($value2) ){
        // The HTML Structure
        $html_output = '<h2>' . __('Extra data') . '</h2>
        <div class="discount-info"><table cellspacing="0" cellpadding="6">';
        if( isset($value1) ){
            $html_output .= '<tr><th>' . $label1 . '</th><td>' . $value1 . '</td></tr>';
        }
        if( isset($value2) ){
            $html_output .= '<tr><th>' . $label2 . '</th><td>' . $value2 . '</td></tr>';
        }
        $html_output .= '</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>';
    }
    $whatsapp_list = $order->get_meta('_whatsapp_list');
    $value = $whatsapp_list === 'Yes' ? 'Yes' : 'No';
    echo '<p><strong>'.__('Added to WhatsApp list?').'</strong> ' . $value . '</p>';
}

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