Add VAT on programmatically created order and user

Question

Im using Gravity forms to create an WooCommerce order programmatically.

If email added to the form already exists, it takes the shipping and billing from the users account. This works and it adds the VAT correctly.

For a user that is not logged in, they add name, email, phone and country, and the user is created automatically. Te order is created correct, and the user is added. But in this case, the AT is not added and I have to manually recalculate the order for the VAT to be added.

How do I make sure the VAT is added in this case also?

code:

add_action( 'gform_after_submission_16', '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);

    // Make sure to add hidden field somewhere in the form with product id and define it here, If you have some other way of defining products in the form you need to make sure product id is returned in the variable below somehow
    $product_id = rgar( $entry, '21' );
 

    $address = array(
      'first_name' => rgar( $entry, '20.3' ),
      'last_name'  => rgar( $entry, '20.6' ),
      'email'      => rgar( $entry, '10' ),
      'phone'      => rgar( $entry, '16' ),
      'address_1'  => rgar( $entry, '24.1' ),
      'address_2'  => rgar( $entry, '24.2' ),
      'city'       => rgar( $entry, '24.3' ),
      'state'      => rgar( $entry, '24.4' ),
      'postcode'   => rgar( $entry, '24.5' ),
      'country'    => rgar( $entry, '24.6' ),
    );

    
    $order = wc_create_order();
    $order->add_product( wc_get_product($product_id), 1); 
    $order->set_address( $address, 'billing' );  
    $country  = rgar( $entry, '24.6' );
    $username = rgar( $entry, '20.3' ); 
    $user_email = rgar( $entry, '10' );   
    
    $user_id = username_exists( $username );
    if ( !$user_id and email_exists($user_email) == false ) {
    $random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
    $user_id = wp_create_user( $username, $random_password, $user_email );    
    } else {
    $random_password = __('User already exists.  Password inherited.'); 
    $user = get_user_by( 'ID', $user_id );
    $address_1 = get_user_meta( $user_id, 'billing_address_1', true );
    $address_2 = get_user_meta( $user_id, 'billing_address_2', true );
    $city      = get_user_meta( $user_id, 'billing_city', true );
    $postcode  = get_user_meta( $user_id, 'billing_postcode', true );
    $country   = get_user_meta( $user_id, 'billing_country', true );
    $state     = get_user_meta( $user_id, 'billing_state', true );
    
    $address_1_ship = get_user_meta( $user_id, 'shipping_address_1', true );
    $address_2_ship = get_user_meta( $user_id, 'shipping_address_2', true );
    $city__ship  = get_user_meta( $user_id, 'shipping_city', true );
    $postcode_ship  = get_user_meta( $user_id, 'shipping_postcode', true );
    $country_ship   = get_user_meta( $user_id, 'shipping_country', true );
    $state_ship     = get_user_meta( $user_id, 'shipping_state', true );

    $address2         = array(
        'address_1'  => $address_1,
        'address_2'  => $address_2,
        'city'       => $city,
        'state'      => $state,
        'postcode'   => $postcode,
        'country'    => $country,
    );
        $address3         = array(
        'address_1'  => $address_1_ship,
        'address_2'  => $address_2_ship,
        'city'       => $city_ship,
        'state'      => $state_ship,
        'postcode'   => $postcode_ship,
        'country'    => $country_ship,
    );
    
    $order->set_address( $address2, 'billing' );
    $order->set_address( $address3, 'shipping' );
       
    }
    $order->set_customer_id( $user_id );
    $note = rgar( $entry, '5' ); ;
    $order->add_order_note( $note );
    $order->calculate_totals();
    $order->update_status("enquiry", TRUE);  

}
0
user3344734 2 months 0 Answers 13 views 0

Leave an answer