Create woocommerce order with gravity forms metadata

I have a interesting and bit complicated task to do with gravity forms and Woocommerce.

On our website, we offer free sample to every new customer registered. We tried to build something like as soon as new user fill their details like email, name and address as well as the free sample they want to get and submit, it should create an order in Woocommerce. Is it possible with gravity forms or its not possible at all?

I looked everywhere for help. Could not find any docs and tried myself, it didn't work for me. any suggestions would be helpful, thanks

1 answer

  • answered 2017-11-14 23:51 Ali_k

    It's possible, but there are a lot of variables that need to be taken into consideration, so it's really hard to help without you doing an attempt first.

    This will only show you where to start, you need to figure out the rest:

    add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
    function post_to_third_party( $entry, $form ) {
        global $woocommerce;
        // use this to find out $entry output
        var_dump($entry);
    
        // Now retrieve product id and the rest of the details you need to create an order
        $product_id = rgar( $entry, '1' );
    
    
    
        $address = array(
          'first_name' => rgar( $entry, '1' )
          'last_name'  => rgar( $entry, '1' )
          'company'    => rgar( $entry, '1' )
          'email'      => rgar( $entry, '1' )
          'phone'      => rgar( $entry, '1' )
          'address_1'  => rgar( $entry, '1' )
          'address_2'  => rgar( $entry, '1' )
          'city'       => rgar( $entry, '1' )
          'state'      => rgar( $entry, '1' )
          'postcode'   => rgar( $entry, '1' )
          'country'    => rgar( $entry, '1' )
        );
    
        $order = wc_create_order();
    
        $order->add_product( get_product($product_id), 1); 
        $order->set_address( $address, 'billing' );
        $order->calculate_totals();
        $order->update_status("processing", 'Sample Order', TRUE);  
    }