添加电子邮件tPHP代码

时间:2017-12-22 02:50:40

标签: php

我今天早些时候在这里找到了这段代码,我尝试修改它但是当我尝试向代码添加更多电子邮件时(我从2开始)我得到一个错误代码,不知道我做错了什么。 / p>

以下是示例

这是我修改后的代码(尝试在底部添加第三封电子邮件,我收到了php错误)

// 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'       => __('Select Location', 'woocommerce'),
        'required'    => true,
        'options'     => array(
            ''   => __('Please select an option', 'woocommerce' ),
            '4289 boul St-Jean' => __('4289 boul St-Jean', 'woocommerce' ),
'3559 boul St-Charles' => __('3559 boul St-Charles', '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 ) {

    $location = get_post_meta( $order->get_id(), '_restaurant_location', true );
    $recipient = $location == '4289 boul St-Jean' ? ',nicks@mtygroup.com' : ',nicsoti@yahoo.com' : ',nicks@mtygroup.com' ;
    return $recipient;
}

这是我找到的原始代码

// 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 ) {

    $location = get_post_meta( $order->get_id(), '_restaurant_location', true );
    $recipient = $location == 'New Orleans' ? ',test1@example.com' : ',test1@example.com';
    return $recipient;
}

1 个答案:

答案 0 :(得分:0)

更改此行:

$recipient = $location == '4289 boul St-Jean' ? ',nicks@mtygroup.com' : ',nicsoti@yahoo.com' : ',nicks@mtygroup.com' ;

到:

switch ($location) {
    case '4289 boul St-Jean':
        $recipient = ',nicks@mtygroup.com';
        break;

    case '3559 boul St-Charles':
        $recipient = ',nicsoti@yahoo.com';
        break;

    default:
        $recipient = ',nicks@mtygroup.com';
        break;
}

或者只是修改它以满足您的需求