Bugcheck my simple wordpress automation

Job ID: 37331936

Budget: $30 – $250 USD

*Important* Please when replying let me know you understand the code snippet and make a relevant reply.
Bot Bids will be ignored .


I need assistance with automating a process on my WordPress website. Here are the specific details and requirements:

Objective:
I often travel and sell products using a POS system called Zettle. When a sale is made, Zettle creates an order in WooCommerce via API, auto-setting it to "complete". However, sometimes I want to sell a customer something I don't have on hand and ship it to them later.

Solution:
I'm using the Forminator Forms plugin in WordPress. My idea is to use Forminator to create a form that captures essential customer details and the Zettle order number. When this form is submitted, I want to search the last 10 WooCommerce orders, find the one that matches the Zettle number, update its status, and add the customer data.

Form Fields:

Zettle order number (text field)
First name (text field)
Last name (text field)
Email (email field)
Shipping address (multiple text fields, see the code below to capture)
Functional Requirements:

On form submission, fetch the Zettle order number and other customer details.
Search the last 10 WooCommerce orders with a status of "completed".
If an order matches the Zettle order number, update the order:
Change the order status to "processing".
Update the billing details with the customer data from the form.
Log every important step, including:
Form submission detected
Form data fetched and sanitized
Last 10 WooCommerce orders fetched
Match found or not found
Order update success or failure
Logging:
All logs should be stored in a way that can be easily accessed via WooCommerce's logging system, specifically under /wp-admin/admin.php?page=wc-status&tab=logs.

Security and Best Practices:
Ensure that the code follows WordPress best practices, is secure, and is efficient.


here is an example of how to zettle order looks in the woocomemrce order page:
#69822 Izettle card sale 1890. in this example the zettle order number would be 1890.


the form ID is 43539


this is my code so far and i can't get it to work. I feel that i am very very close but im missing something and need a bit of help/guidance.




add_action('forminator_form_after_handle_submit', 'my_form_submit', 10, 2);
add_action('forminator_form_after_save_entry', 'my_form_submit', 10, 2);

function my_form_submit($form_id, $response) {
if ($response && is_array($response)) {
if ($response['success']) {
// Check if the form being submitted is our target form
$target_form_id = 43539;
if ($form_id !== $target_form_id) {
return;
}

// Get the latest entry data
$entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id($form_id);

// Fetch and sanitize form data from the $entry object
$zettle_order_number = sanitize_text_field($entry->get_field('text-1'));
$name_data = $entry->get_field('name-1');
$first_name = sanitize_text_field($name_data['first-name']);
$last_name = sanitize_text_field($name_data['last-name']);
$email = sanitize_email($entry->get_field('email-1'));
$street_address = sanitize_text_field($entry->get_field('text-2'));
$city = sanitize_text_field($entry->get_field('text-4'));
$state = sanitize_text_field($entry->get_field('text-5'));
$zip = sanitize_text_field($entry->get_field('text-3'));

// Initialize WooCommerce logger
$logger = wc_get_logger();
$logger->info('Form submission detected. Form data fetched and sanitized.', array('source' => 'zettle-order-updater'));

// Query last 10 WooCommerce completed orders
$args = array(
'status' => 'completed',
'limit' => 10,
);
$orders = wc_get_orders($args);
$logger->info('Last 10 WooCommerce orders fetched.', array('source' => 'zettle-order-updater'));

// Search for the order with the matching Zettle number
foreach ($orders as $order) {
if (strpos($order->get_order_number(), "Izettle card sale " . $zettle_order_number) !== false) {
// Update order data
$order->set_billing_first_name($first_name);
$order->set_billing_last_name($last_name);
$order->set_billing_email($email);
$order->set_billing_address_1($street_address);
$order->set_billing_city($city);
$order->set_billing_state($state);
$order->set_billing_postcode($zip);
$order->set_status('processing');
$order->save();

$logger->info('Order #' . $order->get_id() . ' updated successfully.', array('source' => 'zettle-order-updater'));
return;
}
}

// If we reach here, no matching order was found
$logger->warning('No matching order found for Zettle number: ' . $zettle_order_number, array('source' => 'zettle-order-updater'));
}
}
}
Related categories: PHP WordPress WooCommerce WordPress Plugin