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部分。
感謝您的幫助!只有兩個注意事項:在update_post_meta($ order_id,'_codice_fiscale',...)和get_post_meta($ order-> get_id(),'_codice_fiscale')中使用這兩個符號時,我必須刪除附加的「_」下劃線符號。 ..)否則數據將不會正確保存在元數據中,並且不會在訂單明細中打印。 – alemarengo
事實上,你應該通過擺脫額外的下劃線符號來編輯你的答案,否則我無法確認你的答案對我有效100%。 :) – alemarengo
@alemarengo好吧,我已經更新了我的答案:) – LoicTheAztec