将用户 ID 分配给在 Woocommerce 3 中以编程方式创建的订单

The*_*het 4 php wordpress orders woocommerce

我正在使用 Woocommerce 在 Wordpress 上管理我的产品,但用户通过 3rd 方服务付款。

我正在尝试手动创建新订单,并将其分配给当前用户。订单确实已创建并记录,但以下代码仅记录项目和时间,没有用户的详细信息:

  global $woocommerce;

  $address = array(
      'first_name' => $_GET['FirstName'],
      'last_name'  => $_GET['LastName'],
      'company'    => $_GET['CompanyName'],
      'email'      => $_GET['EmailAddress'],
      'phone'      => $_GET['MobilePhoneNumber'],
      'address_1'  => '',
      'address_2'  => '',
      'city'       => '',
      'state'      => '',
      'postcode'   => '',
      'country'    => $_GET['countryCode']
  );

  $order = wc_create_order();

  foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
            $item_id = $order->add_product(
                    $values['data'], $values['quantity'], array(
                'variation' => $values['variation'],
                'totals' => array(
                    'subtotal' => $values['line_subtotal'],
                    'subtotal_tax' => $values['line_subtotal_tax'],
                    'total' => $values['line_total'],
                    'tax' => $values['line_tax'],
                    'tax_data' => $values['line_tax_data'] // Since 2.2
                )
                    )
            );
        }
  $order->set_address( $address, 'billing' );
  //
  $order->calculate_totals();
  $order->update_status("Completed", 'Imported order', TRUE);  
$order->save();
Run Code Online (Sandbox Code Playgroud)

管理员可以看到订购了什么(在 woocommerce->orders 下),但看不到由谁订购,而用户根本看不到他的订单。

我已将该网站上传到临时域,以便你们可以看到:https : //360transformations.000webhostapp.com/?page_id=446

小智 6

将用户 ID 分配给订单,使用以下选项

选项1:

$userid = get_current_user_id();
$order = wc_create_order(array('customer_id'=>$userid));
Run Code Online (Sandbox Code Playgroud)

选项 2:

在 wc_create_order() 函数之后添加以下代码

$userid = get_current_user_id();
update_post_meta($order->id, '_customer_user', $userid);
Run Code Online (Sandbox Code Playgroud)