Search code examples
wordpresswoocommercegravity-forms-plugin

Override Woocommerce billing fields from Gravity Form


I'm trying to override billing and shipping default woocommerce values with input field ones from a gravity form set on each product of a store.

I can reach gravity form field values executing gform_after_submission_1 hook. My doubt is how to override billing fields.

My code logic tries to work as next:

//Hook action when selects payment and submits order
add_action("gform_after_submission_1", "after_submission", 20, 3);
function after_submission($entry, $form, $fields){
    /*Here is where I want to call woocommerce_checkout_fields filter
     passing $entry form field input values*/
}

// Override checkout fields
add_filter("woocommerce_checkout_fields" , "custom_override_checkout_fields", 10, 1);
//And here is where I override billing values
function custom_override_checkout_fields($entry) {
    $fields['billing']['billing_first_name']['default'] = $entry['14.3'];
    return $fields;
}

Solution

  • Finally, I've found a solution.

    First of all, I need to locate the correct hook to override the order billing and shipping fields. So, I made some magic inside woocommerce_checkout_create_order hook.

    add_action( 'woocommerce_checkout_create_order', 'override_checkout_fields', 10, 2 );
    function override_checkout_fields( $order, $data ){
        //Do your magic here
    }
    

    Then I've located where the Gravity Form fields are 'stored' after add to cart and finally, set the billing and shipping fields from 'captured' ones.

    Code looks something like this one:

    add_action( 'woocommerce_checkout_create_order', 'override_checkout_fields', 10, 2 );
    function override_checkout_fields( $order, $data ){
    
        $cart = WC()->cart->get_cart();
        $field_name_billing = $values['_gravity_form_lead']['17.3']
        $field_name_shipping = $values['_gravity_form_lead']['17.3']
        //... rest of fields
    
        $order->set_billing_field_name($field_name_billing);
        $order->set_shipping_field_name($field_name_shipping);
    }