2015-07-18 27 views
5

Wordpress - Woocommercewoocommerce thêm lĩnh vực tùy chỉnh kiểm tra email

Những gì tôi cố gắng làm:

  • Thêm một số lĩnh vực tùy chỉnh trong trang Checkout.
  • Ngoài ra, hãy gửi giá trị của các trường này đến email của tôi khi nhận được đơn đặt hàng mới.

Đây là những gì tôi đã làm, nhưng thất bại:

Tôi làm theo các hướng dẫn ở đây: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

Tất cả mọi thứ đi đúng, tôi đã tạo các lĩnh vực thành công và chúng xuất hiện trong trang thiết lập thứ tự:

add_action('woocommerce_after_order_notes', 'my_checkout_fields'); 

function my_checkout_fields($checkout) { 

echo '<div id="my_checkout_fields"><h2>' . __('My Heading') . '</h2>'; 

woocommerce_form_field('my_field', array(
    'type'  => 'select', 
    'options' => array(
     'option_1' => 'My Option 1', 
     'option_2' => 'My Option 2' 
    ), 
    'clear'  => false, 
    'class'   => array('form-row-wide'), 
    'label'   => __('Whatever') 
    ), $checkout->get_value('my_field')); 

echo '</div>'; 

} 

/** 
* Update the order meta with field value 
*/ 
add_action('woocommerce_checkout_update_order_meta','my_custom_checkout_field_update_order_meta'); 

function my_custom_checkout_field_update_order_meta($order_id) { 
if (! empty($_POST['my_field'])) { 
    update_post_meta($order_id, 'My Field', sanitize_text_field($_POST['my_field'])); 
} 
} 
} 

Sau đó, tôi đã cố gắng thêm chúng vào meta thứ tự email như được hiển thị ở phần 'Bài học 4' trong hướng dẫn:

add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys'); 
function my_custom_checkout_field_order_meta_keys($keys) { 
    $keys[] = 'my_field'; 
    return $keys; 
} 

Nhưng, trường tùy chỉnh không xuất hiện trong email được gửi.

Tôi cũng đã cố gắng để kiểm tra nó bằng cách làm này trong mẫu email file:

<p><?php echo $order->my_field; ?></p> 

Không có gì được in trong email.

Vui lòng giải pháp. Cảm ơn trước.

+0

bạn gửi mã của bạn xin vui lòng dễ dàng như vậy để nói vấn đề này –

+0

Kiểm tra các ý kiến ​​nêu trên mã trong này link..https: //gist.github.com/mikejolley/1547491 – WisdmLabs

+0

Cảm ơn bạn đã trả lời. Tôi cũng đăng các mã của mình để tạo trường trên trang Checkout ở trên. @ WisdmLabs Vâng, đó là hướng dẫn tôi đã theo dõi, nhưng nó không hoạt động. Điều duy nhất tôi nghĩ rằng tôi có thể đã làm sai là 'Mã theo dõi', nhưng không phải là 'my_field' trong trường hợp của tôi? – Wei

Trả lời

4

Bạn nên sử dụng 'Trường của tôi' thay vì my_field. Khi bạn đã lưu trường tùy chỉnh bằng khóa này. Điều này chắc chắn sẽ giải quyết vấn đề của bạn.

0

Tôi đã gặp sự cố tương tự khi in kết quả cho đến khi tôi thêm dấu gạch dưới trước 'my_field'.

Vì vậy, sử dụng

add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys'); 
function my_custom_checkout_field_order_meta_keys($keys) { 
    $keys[] = '_my_field'; 
    return $keys; 
} 

có vẻ như khi woocommerce được truy vấn dữ liệu meta để sử dụng trong phần admin, một dấu gạch dưới được thêm vào. Vì vậy, đối với một lĩnh vực mà bạn đã tạo như

add_filter('woocommerce_checkout_fields' , 'brown_remove_billing_postcode_checkout'); 

function brown_remove_billing_postcode_checkout($fields) { 

    // Add New Fields 
    $fields['billing']['billing_delivery_date'] = array(
     'label'  => __('Delivery Date', 'woocommerce'), 
     'placeholder' => _x('yyyy-mm-dd', 'placeholder', 'woocommerce'), 
     'required' => true, 
     'class'  => array('form-row-wide'), 
     'clear'  => true, 
     'autocomplete' => false 
    ); 

//Update the order meta with field value. Don't miss the underscore before _b 
$fields['billing_delivery_date'] = get_post_meta($order->id, '_billing_delivery_date', true); 

    return $fields; 
} 

Bạn cần phải thêm vào functions.php

/*---------------------------------------------- 
    Add Delivery Date Field to the order emails 
-------------------------------------------------*/ 
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys'); 

function my_woocommerce_email_order_meta_keys($keys) { 
    $keys['Delivery Date'] = '_billing_delivery_date'; 
    return $keys;  
} 
Các vấn đề liên quan