通过Woocommerce购买/结帐商品时,我的结帐代码包含2个隐藏字段,这些字段发送保存在_dispatch
和_dispatch_driver
下的默认帖子元值。
此帖子元将为选择字段的订单项的管理端提供第一个值。
我有自定义帖子类型,其信息应将选择选项字段作为其他选项提供。
打开订单商品后,我的问题在管理员端。
代码部分来自:Custom editable field in Woocommerce admin edit order pages general section
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'] ) );
}
答案 0 :(得分:1)
由于您的签出字段是具有始终相同值的隐藏字段,因此您不需要它们。您只需要将它们直接保存为订单元数据,并且两者都带有“未分配”值即可。现在,有一个更好的挂钩:
add_action( 'woocommerce_checkout_create_order', 'save_custom_checkout_field', 10, 2 );
function save_custom_checkout_field( $order, $data ) {
$order->update_meta_data( '_dispatch', 'unassigned' );
$order->update_meta_data( '_dispatch_driver', 'unassigned' );
}
现在您的第二个选择字段未保存,因为选择字段中出现错误……我也删除了所有不需要的代码并进行了一些更改:
// Output a custom editable field in backend edit order pages under general section
add_action('woocommerce_admin_order_data_after_order_details', 'editable_order_custom_field', 10, 1 );
function editable_order_custom_field( $order ){
// Display the custom editable field
woocommerce_wp_select( array(
'id' => '_dispatch',
'label' => __("Dispatch Reference:", "woocommerce"),
'type' => 'select',
'options' => array (
'unassigned' => __('Unassigned Dispatch No', '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 (
'unassigned' => __('Unassigned Driver', 'woocommerce' ),
'kamwokya' => __('Kamwokya', 'woocommerce' ),
'bukoto' => __('Bukoto', 'woocommerce' )
),
) );
}
// Save the custom editable field value as order meta data and update order item meta data
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta_data', 12, 2 );
function save_order_custom_field_meta_data( $post_id, $post ){
if( isset( $_POST[ '_dispatch' ] ) ){
update_post_meta( $post_id, '_dispatch', esc_attr( $_POST[ '_dispatch' ] ) );
}
if( isset( $_POST[ '_dispatch_driver' ] ) ){
update_post_meta( $post_id, '_dispatch_driver', esc_attr( $_POST[ '_dispatch_driver' ] ) );
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。