При покупке / оформлении заказа товара (ов) через Woocommerce, мой код проверки включает в себя 2 скрытых поля, которые отправляют мета-значение по умолчанию, сохраненное в _dispatch
и _dispatch_driver
.
Эта мета-публикация отправляет поле выбора с первыми значениями на стороне администратора позиции заказа.
У меня есть пользовательские типы сообщений с информацией, которая должна заполнять поля опций выбора как другие опции.
Мои проблемы на стороне администратора, когда позиция заказа открыта.
- Информация по умолчанию задана правильно, но я могу сохранить / изменить второе поле выбора при нажатии кнопки обновления.
- Я не могу заполнить поля опций woocommerce своими значениями из запросов CPT, которые я сделал.
Код сделан частично из: Настраиваемое редактируемое поле в общем разделе страниц редактирования прав администратора Woocommerce
add_action( 'woocommerce_checkout_update_order_meta', 'save_custom_checkout_field'), 10, 1 );
add_action('woocommerce_admin_order_data_after_order_details', 'editable_order_custom_field', 10, 1 );
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta_data', 12, 2 );
//Then you will need to save this hidden field in the order, this way:
function save_custom_checkout_field( $order_id ) {
if ( ! empty( $_POST['dispatch'] ) )
update_post_meta( $order_id, '_dispatch', sanitize_text_field( $_POST['dispatch'] ) );
if ( ! empty( $_POST['dispatch_driver'] ) )
update_post_meta( $order_id, '_dispatch_driver', sanitize_text_field( $_POST['dispatch_driver'] ) );
}
// Output a custom editable field in backend edit order pages under general section
function editable_order_custom_field( $order ){
// Loop through order items
foreach( $order->get_items() as $item_id => $item ){
// Get "customer reference" from order item meta data
if( $item->get_meta('_dispatch') ){
// The "customer reference"
$item_value = $item->get_meta('_dispatch');
// We output a hidden field with the Item ID (to be able to update this order item meta data later)
echo '<input type="hidden" name="item_id" value="' . $item_id . '">';
break; // We stop the loop
}
if( $item->get_meta('_dispatch_driver') ){
// The "customer reference"
$item_value_ref = $item->get_meta('_dispatch_driver');
// We output a hidden field with the Item ID (to be able to update this order item meta data later)
echo '<input type="hidden" name="item_id_ref" value="' . $item_value_ref . '">';
break; // We stop the loop
}
}
// Get "customer reference" from meta data (not item meta data)
$dispatch_updated_value = $order->get_meta('_dispatch');
$dispatch_driver_updated_value = $order->get_meta('_dispatch_driver');
// Replace "dispatch reference" value by the meta data if it exist
$dispatch_new_value = $dispatch_updated_value ? $dispatch_updated_value : ( isset($item_value) ? $item_value : '');
$dispatch_driver_new_value = $dispatch_driver_updated_value ? $dispatch_driver_updated_value : ( isset($item_value_ref) ? $item_value_ref : '');
$variable = $data_dispatches )[0];
// Display the custom editable field
woocommerce_wp_select( array(
'id' => 'dispatch',
'label' => __("Dispatch Reference:", "woocommerce"),
'type' => 'select',
'options' => array (
//$variable
$dispatch_new_value => __( $dispatch_new_value, 'woocommerce' ),
'Unassigned' => __('Unassigned', 'woocommerce' ),
'nagulu' => __('Nagulu', 'woocommerce' ),
'kamwokya' => __('Kamwokya', 'woocommerce' ),
'bukoto' => __('Bukoto', 'woocommerce' )
)
) );
woocommerce_wp_select( array(
'id' => 'dispatch-driver',
'label' => __("Dispatch Driver:", "woocommerce"),
'type' => 'select',
'options' => array (
$dispatch_driver_new_value => __( $dispatch_driver_new_value, 'woocommerce' ),
'Unassigned' => __('Unassigned', 'woocommerce' ),
'kamwokya' => __('Kamwokya', 'woocommerce' ),
'bukoto' => __('Bukoto', 'woocommerce' )
),
) );
}
// Save the custom editable field value as order meta data and update order item meta data
function save_order_custom_field_meta_data( $post_id, $post ){
if( isset( $_POST[ 'dispatch' ] ) ){
// Save "dispatch reference" as order meta data
update_post_meta( $post_id, '_dispatch', sanitize_text_field( $_POST[ 'dispatch' ] ) );
// Update the existing "dispatch reference" item meta data
if( isset( $_POST[ 'item_id' ] ) )
wc_update_order_item_meta( $_POST[ 'item_id' ], 'Dispatch No.', $_POST[ 'dispatch' ] );
}
if( isset( $_POST[ 'dispatch_driver' ] ) ){
// Save "dispatch_driver reference" as order meta data
update_post_meta( $post_id, '_dispatch_driver', sanitize_text_field( $_POST[ 'dispatch_driver' ] ) );
// Update the existing "dispatch_driver reference" item meta data
if( isset( $_POST[ 'item_id_ref' ] ) )
wc_update_order_item_meta( $_POST[ 'item_id_ref' ], 'Dispatch Driver', $_POST[ 'dispatch_driver' ] );
}
}
}
Это код для сохранения начальных данных на этапе оформления заказа
function register() {
add_action( 'woocommerce_after_order_notes', array( $this, 'my_custom_checkout_field' ), 10, 1 );
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_custom_checkout_field'), 10, 1 );
}
//Then you will need to save this hidden field in the order, this way:
function my_custom_checkout_field( $checkout ) {
$dispatch = 'Unassigned Dispatch No';
$dispatch_driver = 'Unassigned Driver';
// Output the hidden link
echo '
<div id="dispatch_checkout_field">
<input type="hidden" class="input-hidden" name="dispatch" id="dispatch" value="' . $dispatch . '">
</div>
<div id="dispatch_driver_checkout_field">
<input type="hidden" class="input-hidden" name="dispatch_driver" id="dispatch_driver" value="' . $dispatch_driver . '">
</div>
';
}
//Then you will need to save this hidden field in the order, this way:
function save_custom_checkout_field( $order_id ) {
if ( ! empty( $_POST['dispatch'] ) )
update_post_meta( $order_id, '_dispatch', sanitize_text_field( $_POST['dispatch'] ) );
if ( ! empty( $_POST['dispatch_driver'] ) )
update_post_meta( $order_id, '_dispatch_driver', sanitize_text_field( $_POST['dispatch_driver'] ) );
}