添加电子邮件tPHP代码

问题描述 投票:-1回答:1

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

这是一个例子

这是我修改过的代码(尝试在底部添加第三封电子邮件,我收到了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' ? ',[email protected]' : ',[email protected]' : ',[email protected]' ;
    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' ? ',[email protected]' : ',[email protected]';
    return $recipient;
}
php
1个回答
0
投票

改变这一行:

$recipient = $location == '4289 boul St-Jean' ? ',[email protected]' : ',[email protected]' : ',[email protected]' ;

至 :

switch ($location) {
    case '4289 boul St-Jean':
        $recipient = ',[email protected]';
        break;

    case '3559 boul St-Charles':
        $recipient = ',[email protected]';
        break;

    default:
        $recipient = ',[email protected]';
        break;
}

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

© www.soinside.com 2019 - 2024. All rights reserved.