【发布时间】:2021-02-16 02:11:45
【问题描述】:
我正在使用 Gravity 表单以编程方式创建 WooCommerce 订单。
如果添加到表单的电子邮件已经存在,它将从用户帐户中获取运费和账单。这行得通,并且正确添加了增值税。
对于未登录的用户,他们会在表单中添加姓名、电子邮件、电话和国家,然后自动创建用户。订单已创建,用户已添加。但在这种情况下,AT 没有添加,我必须手动重新计算要添加的增值税的顺序。
如何确保在以编程方式创建用户时添加增值税?
代码:
add_action( 'gform_after_submission_16', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
global $woocommerce;
var_dump($entry);
$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 );
$firstname = get_user_meta( $user_id, 'billing_first_name', true );
$lastname = get_user_meta( $user_id, 'billing_last_name', true );
$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 );
$firstname_ship = get_user_meta( $user_id, 'shipping_first_name', true );
$lastname_ship = get_user_meta( $user_id, 'shipping_last_name', 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(
'first_name' => $firstname,
'last_name' => $lastname,
'address_1' => $address_1,
'address_2' => $address_2,
'city' => $city,
'state' => $state,
'postcode' => $postcode,
'country' => $country,
);
$address3 = array(
'first_name' => $firstname_ship,
'last_name' => $lastname_ship,
'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->set_customer_note( $note );
$emails = WC()->mailer()->get_emails();
$emails['WC_Email_Customer_New_Account']->trigger( $user_id, $random_password, true );
$order->calculate_totals();
$order->update_status('autoquote', TRUE);
$order->save();
}
编辑:
好像我的新用户没有正确创建,他们缺少姓名、电话和国家。我加了
wp_update_user([
'ID' => $user_id,
'first_name' => rgar( $entry, '20.3' ),
'last_name' => rgar( $entry, '20.6' ),
'phone' => rgar( $entry, '16' ),
'country' => rgar( $entry, '24.6' )
]);
$user_id = wp_create_user( $username, $random_password, $user_email ); 之后
这会为用户设置正确的姓名,但仍然缺少电话和国家/地区。
【问题讨论】:
标签: php wordpress woocommerce orders user-data