【问题标题】:Email recipient based on a custom field value in WooCommerce基于 WooCommerce 中自定义字段值的电子邮件收件人
【发布时间】:2018-02-25 00:15:53
【问题描述】:

我在 Woocommerce 商店的结帐页面中添加了一个自定义字段。该字段是“餐厅位置”。在客户下订单后,我的目标是使用“餐厅位置”字段来确定将订单确认发送到哪个电子邮件。

这是我定义自定义字段的方式。

/////// Hook custom field in ///////

add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' );

function custom_checkout_fields( $fields ) {

$fields['order']['restaurant_location'] = array(
'label'       => __('Food options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required'    => true,
'clear'       => false,
'type'        => 'select',
'options'     => array(
    'no' => __('New Orleans', 'woocommerce' ),
    'br' => __('Baton Rouge', 'woocommerce' )
    )
);

 return $fields;

}

这是我对电子邮件过滤器的尝试。

add_filter( 'woocommerce_email_recipient_new_order', 'gon_conditional_email_recipient', 10, 2 );

function gon_conditional_email_recipient( $recipient, $order ) {

$gon_order_data = $order->get_data();
$gon_restaurant_location = $gon_order_data['order']['restaurant_location'];

if (  $gon_restaurant_location == 'New Orleans' ) {
    $recipient = 'test1@gmail.com';
    return $recipient;
} 
else if (  $gon_restaurant_location == 'Baton Rouge' ) {
    $recipient = 'test2@gmail.com';
    return $recipient;
} 

return $recipient;


}

电子邮件过滤器正在工作,即我可以收到一封电子邮件以发送到任一地址,但我似乎无法正确提取“$gon_restaurant_location”变量。有什么想法吗?

谢谢,

ps

【问题讨论】:

    标签: php wordpress woocommerce checkout email-notifications


    【解决方案1】:

    您的自定义字段值未保存在数据库中,因此无法正常工作。试试这个完整的解决方案:

    // Add the custom checkout field
    add_filter( 'woocommerce_after_order_notes', 'restaurant_location_checkout_field' );
    function restaurant_location_checkout_field( $checkout ) {
    
        woocommerce_form_field( 'restaurant_location', array(
            'type'        => 'select',
            'class'       => array('my-field-class form-row-wide'),
            'label'       => __('Food options', 'woocommerce'),
            'required'    => true,
            'options'     => array(
                ''   => __('Please select an option', 'woocommerce' ),
                'New Orleans' => __('New Orleans', 'woocommerce' ),
                'Baton Rouge' => __('Baton Rouge', 'woocommerce' )
            )
        ), $checkout->get_value( 'restaurant_location' ));
    }
    
    // Process the checkout (checking)
    add_action('woocommerce_checkout_process', 'restaurant_location_field_process');
    function restaurant_location_field_process() {
        // Check if set, if its not set add an error.
        if ( ! $_POST['restaurant_location'] )
            wc_add_notice( __( 'Please select a food option .' ), 'error' );
    }
    
    // Update the order meta with field value
    add_action( 'woocommerce_checkout_update_order_meta', 'restaurant_location_field_update_order_meta' );
    function restaurant_location_field_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['restaurant_location'] ) ) {
            update_post_meta( $order_id, '_restaurant_location', sanitize_text_field( $_POST['restaurant_location'] ) );
        }
    }
    
    // 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>'.__('Food options', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_restaurant_location', true ) . '</p>';
    }
    
    // Conditional Email recipient filter based on restaurant location
    add_filter( 'woocommerce_email_recipient_new_order', 'conditional_email_recipient', 10, 2 );
    function conditional_email_recipient( $recipient, $order ) {
        if( is_admin() ) return $recipient;
    
        $location = get_post_meta( $order->get_id(), '_restaurant_location', true );
        $recipient = $location == 'New Orleans' ? ',test1@example.com' : ',test1@example.com';
        return $recipient;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    此代码在 Woocommerce 3+ 上经过测试并且可以正常工作


    基于官方开发者文档:Adding a custom special field

    【讨论】:

    • 我想问一下 OP 是否在任何地方保存信息。虽然我建议切换到 woocommerce_checkout_create_order 以与他们的 CRUD 方法向前兼容。
    • @helgatheviking 好建议 +1 ... WooCommerce 工作人员应该更新他们的结帐字段文档... 即使还有一些剩余的错误,例如get_post_meta( $order-&gt;id
    猜你喜欢
    • 2022-01-26
    • 2021-07-02
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 2022-10-05
    相关资源
    最近更新 更多