在订单电子邮件中发送结账页自定义域

时间:2014-12-01 作者:Sanjay Nakate

我使用下面的代码段挂钩和函数在签出页面中添加了选项框自定义字段。当用户订购产品时,选项框自定义字段值显示在“backed admin”面板中,但我无法将该字段以订单形式发送电子邮件。

我在这个问题的底部添加了用于发送自定义字段值的代码或函数。

任何人都可以指导我发送此自定义字段,以便向用户客户和网站管理员发送电子邮件。

  // Add a new checkout field
            function kia_filter_checkout_fields($fields){
                $fields[\'extra_fields\'] = array(
                        \'some_field\' => array(
                            \'type\' => \'select\',
                            \'required\'      => true,
                            \'label\' => __( \'Some field\' ),
                            \'options\'     => array(
                            \'Newspaper\' => __(\'Newspaper Ad\', \'woocommerce\' ),
                            \'Facebook\' => __(\'Facebook\', \'woocommerce\' ),
                            \'Twitter\' => __(\'Twitter\', \'woocommerce\' ),
                            \'Email\' => __(\'Email\', \'woocommerce\' ),
                            \'Poster\' => __(\'College Poster\', \'woocommerce\' ),
                            \'SMS\' => __(\'SMS\', \'woocommerce\' ),
                            \'Telephone\' => __(\'Telephone Call\', \'woocommerce\' ),
                            \'Friends\' => __(\'Friends\', \'woocommerce\' ),
                            \'Others\' => __(\' Others\', \'woocommerce\' )
                                                   )
                                            )
                                   );

                return $fields;
            }
            add_filter( \'woocommerce_checkout_fields\', \'kia_filter_checkout_fields\' );

            // display the extra field on the checkout form
            function kia_extra_checkout_fields(){ 

                $checkout = WC()->checkout(); ?>

                <div class="extra-fields">
                <h3><?php _e( \'How did you learn about us?\' ); ?></h3>

                <?php foreach ( $checkout->checkout_fields[\'extra_fields\'] as $key => $field ) : ?>

                        <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>

                    <?php endforeach; ?>
                </div>

            <?php }
            add_action( \'woocommerce_after_order_notes\' ,\'kia_extra_checkout_fields\' );

            // save the extra field when checkout is processed
            function kia_save_extra_checkout_fields( $order_id, $posted ){
                if( isset( $posted[\'some_field\'] ) ) {
                    update_post_meta( $order_id, \'_some_field\', 
                  sanitize_text_field( $posted[\'some_field\'] ) );
                }
            }
            add_action( \'woocommerce_checkout_update_order_meta\', 
             \'kia_save_extra_checkout_fields\', 10, 2 );

            // display the extra data in the order admin panel
            function kia_display_order_data_in_admin( $order ){  ?>
                <div class="order_data_column">
                    <h4><?php _e( \'Extra Details\', \'woocommerce\' ); ?></h4>
                    <?php 
                        echo \'<p><strong>\' . __( \'Some field\' ) . \':</strong>\'
         . get_post_meta( $order->id, \'_some_field\', true ) . \'</p>\'; ?>
                </div>
            <?php }
            add_action( \'woocommerce_admin_order_data_after_order_details\', 

    \'kia_display_order_data_in_admin\' );

In bellow snippet Function which i am using to send custom field value in order email

/**
 * Add the field to order emails
 **/
add_filter(\'woocommerce_email_order_meta_keys\', \'my_woocommerce_email_order_meta_keys\');

function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys[\'How did you hear about us?\'] = $field;
    return $keys;
}

2 个回复
最合适的回答,由SO网友:Sanjay Nakate 整理而成

最后我得到了答案,我错了,因为我试图发送变量,你不能发送变量,你只需输入自定义字段slug的名称。

在下面的代码片段中,我替换了$field 使用我的自定义字段名slug_some_field

类似:-$keys[\'How did you learn about us?\'] = \'_some_field\';

/**
 * Add the field to order emails
 **/
add_filter(\'woocommerce_email_order_meta_keys\', \'my_woocommerce_email_order_meta_keys\');

function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys[\'How did you learn about us?\'] = \'_some_field\';
    return $keys;
}

SO网友:Piyush Patel

按以下要求订购Post自定义字段多个字段。

add_filter(\'woocommerce_email_order_meta_keys\', \'my_woocommerce_email_order_meta_keys\');

function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys[] = \'custom_field_slug1\';
    $keys[] = \'custom_field_slug2\';
    $keys[] = \'custom_field_slug3\';
    return $keys;
}
请按照上述步骤进行尝试。

这对我有用。

结束

相关推荐

EMAIL_EXISTS在短码中有问题

我有一个自定义注册表,可以通过快捷码插入页面。shortcode函数如下所示:function custom_registration_form_shortcode() { $error = FALSE; if (isset($_POST[\"submit\"])) { //Get other $_POST data here $email = is_email($_POST[\"email\"