首页 > 解决方案 > 电子邮件通知上的自定义结帐结算字段

问题描述

在 WooCommerce 中,基于“ WooCommerce 中的动态同步自定义结帐选择字段答案代码,我在结帐时添加了一些自定义字段,这些字段将显示在电子邮件确认的计费部分下

这是我的实际代码:

add_action( 'woocommerce_after_checkout_billing_form', 'add_checkout_custom_fields', 20, 1 );
function add_checkout_custom_fields( $checkout) {
    $domain = 'woocommerce'; // The domain slug

    // First Select field (Master)
    woocommerce_form_field( 'delivery_one', array(
        'type'          => 'select',
        'label'         => __( 'Art der Lieferung' , $domain),
        'class'         => array( 'form-row-first' ),
        'required'       => true,
        'options'       => array(
            ''  => __( 'Wählen Art der Lieferung.', $domain ),
            'A' => __( 'Hauszustellung', $domain ),
            'B' => __( 'Selbst Abholung', $domain ),

        ),
    ), $checkout->get_value( 'delivery_one' ) );

    // Default option value
    $default_option2 = __( 'Wählen Sie Zeitbereich.', $domain );

    // Dynamic select field options for Javascript/jQuery
    $options_0 = array( '' => $default_option2 );
    $options_a = array(
        ''  => $default_option2,
        '1' => __( '09:00-11:00', $domain ),
        '2' => __( '10:00-12:00', $domain ),
        '3' => __( '11:00-13:00', $domain ),
        '4' => __( '12:00-14:00', $domain ),
        '5' => __( '13:00-15:00', $domain ),
        '6' => __( '14:00-16:00', $domain ),
        '7' => __( '15:00-17:00', $domain ),
    );

    $options_b = array(
        ''  => $default_option2,
        '1' => __( '01:00', $domain ),
        '2' => __( '02:00', $domain ),
        '3' => __( '03:00', $domain ),
        '4' => __( '04:00', $domain ),
        '5' => __( '05:00', $domain ),
        '6' => __( '06:00', $domain ),
        '7' => __( '07:00', $domain ),
        '8' => __( '08:00', $domain ),
        '9' => __( '09:00', $domain ),
        '10' => __( '10:00', $domain ),
        '11' => __( '11:00', $domain ),
        '12' => __( '12:00', $domain ),
        '13' => __( '13:00', $domain ),
        '14' => __( '14:00', $domain ),
        '15' => __( '15:00', $domain ),
        '16' => __( '16:00', $domain ),
        '17' => __( '17:00', $domain ),
        '18' => __( '18:00', $domain ),
        '19' => __( '19:00', $domain ),
        '20' => __( '20:00', $domain ),
        '21' => __( '21:00', $domain ),
        '22' => __( '22:00', $domain ),
        '23' => __( '23:00', $domain ),
        '24' => __( '24:00', $domain ),
    );

    // Second Select field (Dynamic Slave)
    woocommerce_form_field( 'delivery_two', array(
        'type'          => 'select',
        'label'         => __( 'Zeitspanne', $domain ),
        'class'         => array( 'form-row-last' ),
        'required'       => true,
        'options'       => $options_0,
    ), $checkout->get_value( 'delivery_two' ) );

    $required = esc_attr__( 'required', 'woocommerce' );

    // jQuery code
    ?>
    <script>
    jQuery(function($){
        var op0 = <?php echo json_encode($options_0); ?>,
            opa = <?php echo json_encode($options_a); ?>,
            opb = <?php echo json_encode($options_b); ?>,

            select1 = 'select[name="delivery_one"]',
            select2 = 'select[name="delivery_two"]';

        // Utility function to fill dynamically the select field options
        function dynamicSelectOptions( opt ){
            var options = '';
            $.each( opt, function( key, value ){
                options += '<option value="'+key+'">'+value+'</option>';
            });
            $(select2).html(options);
        }

        // 1. When dom is loaded we add the select field option for "A" value
        // => Disabled (optional) — Uncomment below to enable
        // dynamicSelectOptions( opa );

        // 2. On live selection event on the first dropdown
        $(select1).change(function(){
            if( $(this).val() == 'A' )
                dynamicSelectOptions( opa );
            else if( $(this).val() == 'B' )
                dynamicSelectOptions( opb );

            else
                dynamicSelectOptions( op0 ); // Reset to default
        });
    });
    </script>
    <?php
}

// Check checkout custom fields
add_action( 'woocommerce_checkout_process', 'wps_check_checkout_custom_fields', 20 ) ;
function wps_check_checkout_custom_fields() {
    // if custom fields are empty stop checkout process displaying an error notice.
    if ( empty($_POST['delivery_one']) || empty($_POST['delivery_two']) ){
        $notice = __( 'Bitte wählen Sie die Versandart oder den Stundenbereich' );
        wc_add_notice( '<strong>' . $notice . '</strong>', 'error' );
    }
}  

我的自定义字段及其值显示在结帐表单和后端的订单页面上。到目前为止,一切都很好。

但问题是我收到的电子邮件不包含自定义字段及其值。

如何在电子邮件通知中显示自定义结帐账单字段?

这段代码正确吗?

add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
  function add_custom_email_format_string( $string, $email ) {
// The post meta key used to save the value in the order post meta data
$meta_key = '_delivery_one';

// Get the instance of the WC_Order object
$order    = $email->object;

// Get the value
$value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';

// Additional subject placeholder
$new_placeholders = array( '{delivery_one}' => $value );


return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
 }

标签: phpwordpresswoocommerceemail-notifications

解决方案


您应该首先在数据库中将输入值保存为订单元,请参见此处,然后您可以在电子邮件模板中获取元值。woocommerce 的所有电子邮件模板都是可自定义的,位于plugins/woocommerce/emails/

您可以使用此钩子将自定义输入字段存储在订单元

do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );

谢谢


推荐阅读