I have a function that runs on the front-end of my WooCommerce Wordpress site that generates ticket numbers for people who answer a simple question correctly and no ticket order for those that do not answer correctly. It saves the users answer in the order item meta data and I have it so that the answer is displayed in the backend in an editable WooCommerce Text input field for each item within the order as each item would have a different answer.
add_action( 'woocommerce_admin_order_item_values', 'pd_admin_order_item_values', 10, 3);
function pd_admin_order_item_values( $product, $item, $item_id ) {
$user_answer = $item->get_meta( "User Answer" );
?>
<div class="edit_custom_field"> <!-- use same css class in h4 tag -->
<?php
woocommerce_wp_text_input( array(
'id' => 'custom_field_name',
'label' => 'Update User Answer:',
'value' => $user_answer,
'wrapper_class' => 'form-field-wide'
) );
?>
</div>
<?php
}
I need to make it so that if an admin changes it to the correct field and updates the post it will save the new data and run the function to assign a ticket number. I am using the correct hook I believe as it crashes when I hit update on the order but the logs are saying that it has too few arguments to run. I have seen some people have used $post_id and $post instead of the order variants but changing it to call these variables hasn't worked either.
add_action( 'woocommerce_process_shop_order_meta', 'custom_woocommerce_process_shop_order_meta', 10, 4 );
function custom_woocommerce_process_shop_order_meta( $item, $order_id, $order ){
// Loop through order items
foreach ($item->get_items() as $item_id => $item ) {
$admin_question = wc_sanitize_textarea( $_POST[ 'custom_field_name' ] );
$user_answer = $item->get_meta( $admin_question );
$admin_answer = $item->get_meta( "Answer" );
if( $admin_answer == $user_answer ){
// Check that tickets numbers haven't been generated yet for this item
if( $item->get_meta( "_tickets_number") )
continue;
$product = $item->get_product(); // Get the WC_Produt Object
$quantity = (int) $item->get_quantity(); // Get item quantity
// Get last ticket sold index from product meta data
$now_index = (int) $product->get_meta('_tickets_sold');
$tickets_ids = array(); // Initializing
// Generate an array of the customer tickets Ids from the product registered index (last ticket ID)
for ($i = 1; $i <= $quantity; $i++) {
$tickets_ids[] = $now_index + $i;
};
// Save the tickets numbers as order item custom meta data
$item->update_meta_data( "Tickets numbers", implode(' ', $tickets_ids) ); // Displayed string of tickets numbers on customer orders and emails
$item->update_meta_data( "User Answer", $user_answer ); // Displayed string of tickets numbers on customer orders and emails
$item->update_meta_data( "_tickets_number", $tickets_ids ); // (Optional) Array of the ticket numbers (Not displayed to the customer)
$item->save(); // Save item meta data
// Update the Ticket index for the product (custom meta data)
$product->update_meta_data('_tickets_sold', $now_index + $quantity );
$product->save(); // Save product data
} else {
$item->update_meta_data( "User Answer", $user_answer ); // Displayed string of tickets numbers on customer orders and emails
$item->save(); // Save item meta data
}
}
$order->save(); // Save all order data
}
My issue I believe lies in it not being able to call the order and order id variable when it runs the function but I am unsure.