Ubercart:以编程方式使用产品创建订单 Drupal 8

Ubercart: Create order with products programmatically Drupal 8

如何使用自定义表单中的产品创建订单? 这是我的代码:

$order = Order::create(array(
  'uid' => $uid,
  'order_status' => uc_order_state_default('post_checkout'),
));
$order->save();
uc_order_comment_save($order->id(), $this->currentUser()->id(), $this->t('Order created by the administration.'), 'admin');
$product = \Drupal\node\Entity\Node::load($nid);
uc_order_product_save($order->id(), $product);

订单已保存....但我的订单中没有相关产品。请帮忙!

我使用 drupal 8.3 和 Ubercart 8.x-4.0-alpha5

我也不明白为什么你的代码不起作用...
但我找到了将产品与订单 ID 相关联的解决方案。

尝试使用此代码将产品分配给订单 ID。

$product = Drupal::entityTypeManager()->getStorage('uc_order_product')->create(array(
'qty' => 1,
'order_id' => $order->id(),
'nid' => $nid,
));

$product->save();