在WooCommerce中以编程方式创建带有增值税的新订单

时间:2020-11-02 15:04:14

标签: php woocommerce

我正在尝试通过提交表单创建新订单,效果很好。唯一的问题是订单设置没有增值税,我必须手动按重新计算订单才能显示。

如何根据用户所在国家/地区以编程方式更新总税额?我尝试了发现的其他税率版本,但似乎没有任何效果。正确的方法是什么?

代码:

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' ),
    );
     $address2 = array(
      'address_1'  => get_user_meta( $user_id, 'address_1', true ),
      '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' );  
    $order->set_address( $address, 'shipping' );  
    $order->calculate_totals();
    $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 );
    $address2         = array(
        'address_1'  => $address_1,
        'address_2'  => $address_2,
        'city'       => $city,
        'state'      => $state,
        'postcode'   => $postcode,
        'country'    => $country,
    );
    $order->set_address( $address2, 'billing' );
    $order->set_address( $address2, 'shipping' );

    }
    $order->set_customer_id( $user_id );
    $note = rgar( $entry, '5' ); ;
    $order->add_order_note( $note );
    $order->calculate_totals();
    $order->update_status("enquiry", TRUE);  

}

编辑:

通过添加解决

$order->calculate_taxes( $country );

之前

$order->calculate_totals();

0 个答案:

没有答案