I have the snippet to change the woocommerce order status field from pending to completed, which I will post below.This woks fine. My problem is that I now have a second product that uses different form, with the same woocommerce status field that needs to to be triggered to "completed" after payment. If I simply add a copy of the snippet with different form and field id's, it crashes the site. I imagine there must be code to update this status field for sites with multiple forms and multiple products that use the same payment gateway. Any advice greatly appreciated!
add_action( 'woocommerce_order_status_completed', 'update_frm_entry_after_wc_order_completed' );
function update_frm_entry_after_wc_order_completed( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $product ) {
if ( isset( $product['formidable_form_data'] ) && is_numeric( $product['formidable_form_data'] ) ) {
$entry_id = $product['formidable_form_data'];
$entry = FrmEntry::getOne( $entry_id );
if ( $entry && $entry->form_id == 73 ) {
$field_id = 1651;//Replace with the field ID to update
$new_value = 'Complete';
FrmEntryMeta::update_entry_meta( $entry_id, $field_id, null, $new_value );
}
}
}
}
BTW the second form id is 78, and the field id is 1720
You only need to change 2 lines of code in this snippet to make it work with two forms. If you need it to work for more than 2 forms, the approach will be different.
For 2 forms change:
if ( $entry && $entry->form_id == 73) {
To:
if ( $entry && ( $entry->form_id == 73 || $entry->form_id == 78 ) ) {
And also change:
$field_id = 1651;
TO:
$field_id = ( $entry->form_id == 73 ) ? 1651 : 1720;
Thanks for your response! I tried changing the code, and I'm sure it's easy for who knows php, but everything I try crashes the site. If it's not too much to ask, could you be more specific on the change that needs to be made?
I can't be more specific than what I've already provided. I gave you the actual code and the lines to change. What's more specific than the actual changes that you already have?
Sorry Victor didn't read your response correctly! My bad. Thank you again, trying your code now.
Thank you, Victor! Worked like a charm...
Please login or Register to submit your answer