Woocommerce order status

By: rob anthony | Asked: 02/07/2023
ForumsCategory: General questionsWoocommerce order status
rob anthony asked 2 years ago

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

1 Answers
Victor Font answered 2 years ago

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.

Victor Font replied 2 years ago

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;

rob anthony replied 2 years ago

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?

Victor Font replied 2 years ago

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?

rob anthony replied 2 years ago

Sorry Victor didn't read your response correctly! My bad. Thank you again, trying your code now.

rob anthony replied 2 years ago

Thank you, Victor! Worked like a charm...

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right