我正在嘗試將可編輯的自定義欄位添加到管理頁面順序中。這是一個汽車服務網站,我需要在訂單完成時插入總汽車公里數。我可以在頁面上看到該欄位,但在我更新訂單時它不會保存并顯示任何值。這是我添加到 function.php 的代碼:
add_action( 'woocommerce_admin_order_data_after_order_details', 'editable_order_custom_field', 12, 1 );
function editable_order_custom_field( $order ){
// custom editable field
woocommerce_wp_text_input( array(
'id' => 'custom_km',
'label' => __("Total Km:", "woocommerce"),
'value' => $value,
'wrapper_class' => 'form-field-wide',
) );
}
非常感謝任何幫助。
uj5u.com熱心網友回復:
您需要將資料保存到資料庫中。
add_action( 'woocommerce_process_shop_order_meta', 'save_custom_filed_data' );
function save_custom_filed_data( $order_id ){
update_post_meta( $order_id, 'custom_km', wc_clean( $_POST['custom_km'] ) );
}
然后您可以通過以下方式檢索資料。在這里,我在帳單地址后面顯示了資料,您可以通過鉤子將其顯示在所需的位置。
add_action('woocommerce_admin_order_data_after_billing_address', 'display_the_custom_distance_km', 10, 1);
function display_the_custom_distance_km($order)
{
echo '<p><strong>Custom Km:</strong><br>' . get_post_meta($order->get_ID(), 'custom_km', true) . '</p>';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/329699.html
標籤:php WordPress的
下一篇:lumenarray_key_exists()貶值了,那么如何使用isset()或property_exists()之類的替代方法來陣列呢?
