2017-09-15 1 views
1

Woocommerce 저장소의 체크 아웃 페이지에 사용자 지정 필드를 추가했습니다. 필드는 '식당 위치'입니다. 고객이 주문할 때 나의 목표는 '식당 위치'필드를 사용하여 주문 확인을 보낼 이메일을 결정하는 것입니다.전자 메일 수신자가 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 = '[email protected]'; 
    return $recipient; 
} 
else if ( $gon_restaurant_location == 'Baton Rouge') { 
    $recipient = '[email protected]'; 
    return $recipient; 
} 

return $recipient; 


} 

, 즉 나도 주소로 가서 이메일을 얻을 수 있습니다,하지만 난 제대로 '$gon_restaurant_location'변수에 끌어 수없는 것. 어떤 아이디어?

감사합니다,

피코

답변

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

    $location = get_post_meta($order->get_id(), '_restaurant_location', true); 
    $recipient = $location == 'New Orleans' ? ',[email protected]' : ',[email protected]'; 
    return $recipient; 
} 

코드는 어떤 플러그인 파일도 function.php의 활성 자식 테마 (또는 테마)의 파일이나 간다.

이 코드는 Woocommerce 3+ 테스트 및 작동합니다

공식 개발자 용 문서를 기반으로

: 나는 영업 이익은 * 어디 *에서 정보를 저장하는 경우 물어 가고 있었다 Adding a custom special field

+1

. CRUD 메소드와의 호환성을 위해'woocommerce_checkout_create_order'로 전환하는 것이 좋습니다. – helgatheviking

+0

@helgatheviking 좋은 제안 +1 ... WooCommerce 직원이 체크 아웃 필드 문서를 업데이트해야합니다 ... 'get_post_meta ($ order-> id')와 같은 나머지 오류가 있습니다. – LoicTheAztec

관련 문제