2017-04-18 107 views
1

如何在我的自定义表单中创建与产品的订单? 这是我的代码:Ubercart:以编程方式通过产品创建订单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

回答

1

我还没有找到为什么你的代码是不工作...
但我有找到解决涉及产品订购ID。

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

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

$product->save(); 
+0

我做到了!非常感谢你。 –

+0

我无法在扩展FormBase类的自定义窗体中以编程方式执行此操作。我们需要扩展或使用的任何类? –

相关问题