Search code examples
phpwordpresswoocommercehookgravity-forms-plugin

Update order status on form submission


I am trying to create a form that automatically updates order status once form has been submitted. The form is located at the order details page, so I would assume that current page ID is equal to orderID. When I try to do a form submission it's simply just stuck and nothing happens. I am asumming it's a problem with getting the orderID and therefore which order to update status on.

I've found the gform_after_submission hook and linked it to the form that is placed on the order details page (form ID 7). I've been trying to use global $wpdb; but not quite sure if thats the right thing to do.

add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function update_order_submission( $order_id ) {
global $wpdb;

    //getting orderID
    $order = wc_get_order( $order_id );

    //changing order status
                $order = array();
                $order['ID'] = $order->ID;
                $order['post_status'] = 'wc-completed';

    //updating order
    wp_update_post( $order );
}

I am expecting that once a form has been submitted then the order status of the current order ID (the page the form was submitted from) is going to be updated as orderstatus completed.


Solution

  • I managed to solve it by getting the orderID from the URL by using $_GET["id"] to getting the url parameter. The code below in functions.php solves the task.

    add_action( 'gform_after_submission_7', 'update_order_submission', 10, 2 );
    function update_order_submission( $entry, $form ) {
    global $post;
    $order_id = $_GET["id"]; // getting orderID
    $order = wc_get_order( $order_id );
    if( $order ) {
    //changing order status
    $order->update_status( 'completed' );
    }
    }