2017-10-06 98 views
1

我有一個和here類似的東西:我不明白爲什麼這個自定義字段的值沒有保存。這裏是我的代碼:在Woocommerce訂單中顯示一個自定義字段值編輯視圖

add_filter('woocommerce_checkout_fields' , 'altri_campi'); 

function altri_campi($fields) { 
$fields['billing']['codice_fiscale'] = array(
     'class'  => array('form-row-wide'), 
     'label'  => __('Codice Fiscale', 'woocommerce'), 
     'placeholder' => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'), 
     'required' => true, 
     'class'  => array('form-row-wide') 
    ); 

    return $fields; 

}  

// like LoizTheAztec above 
    add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1); 
    function my_custom_checkout_field_update_order_meta($order_id) { 
     if (! empty($_POST['codice_fiscale'])) { 
      update_post_meta($order_id, 'Codice Fiscale', sanitize_text_field($_POST['codice_fiscale'])); 
     } 
    } 

    // then I'm expecting that custom field value will be saved somehow, but won't 

    add_action('woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1); 

    function my_custom_checkout_field_display_admin_order_meta($order){ 
     echo '<p><strong>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . get_post_meta($order->get_id(), '_codice_fiscale', true) . '</p>'; 
    } 

元字段被正確保存和打印,但我不能在訂單數據視圖中添加自定義字段值。

我在做什麼錯了,以及如何在訂單編輯視圖中顯示此自定義字段值?

否則,我希望在客戶的結算和運輸數據後發現的文本是無效的,因爲我只讀取了最新代碼段的html部分。

回答

1

這裏是正確的註釋和解釋代碼:

// Creating and displaying the custom checkout field in checkout page 
add_filter('woocommerce_checkout_fields' , 'altri_campi'); 
function altri_campi($fields) { 
    $fields['billing']['codice_fiscale'] = array(
     'class'  => array('form-row-wide'), 
     'label'  => __('Codice Fiscale', 'woocommerce'), 
     'placeholder' => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'), 
     'required' => true, 
     'class'  => array('form-row-wide') 
    ); 
    return $fields; 
} 

// Saving the custom checkout field value in the order meta data 
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1); 
function my_custom_checkout_field_update_order_meta($order_id) { 
    if (! empty($_POST['codice_fiscale'])){ 
     update_post_meta($order_id, 'codice_fiscale', sanitize_text_field($_POST['codice_fiscale'])); 

     // get the customer ID 
     $customer_id = get_post_meta($order_id, '_customer_user', true); 

     // Update customer user data 
     update_user_meta($customer_id, 'codice_fiscale', true); 
    } 
} 

// Displaying the custom checkout field value in the order edit page (backend) 
add_action('woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1); 
function custom_checkout_field_display_admin_order_meta($order){ 
    $codice_fiscale = get_post_meta($order->get_id(), 'codice_fiscale', true); 
    if(! empty($codice_fiscale)) 
     echo '<p><strong>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . $codice_fiscale . '</p>'; 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

測試和工作。您將獲得在後端訂單編輯頁面類似(下面的地址)

enter image description here

+0

感謝您的幫助!只有兩個注意事項:在update_post_meta($ order_id,'_codice_fiscale',...)和get_post_meta($ order-> get_id(),'_codice_fiscale')中使用這兩個符號時,我必須刪除附加的「_」下劃線符號。 ..)否則數據將不會正確保存在元數據中,並且不會在訂單明細中打印。 – alemarengo

+0

事實上,你應該通過擺脫額外的下劃線符號來編輯你的答案,否則我無法確認你的答案對我有效100%。 :) – alemarengo

+0

@alemarengo好吧,我已經更新了我的答案:) – LoicTheAztec

相關問題