Tan Nguyen
Tan Nguyen

Reputation: 426

Ubercart: Create order with products programmatically Drupal 8

How can i create order with products in my custom form? This my code:

$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);

Order was saved.... But no product related in my order. Please help!

I use drupal 8.3 and Ubercart 8.x-4.0-alpha5

Upvotes: 1

Views: 751

Answers (1)

Ujjval Jha
Ujjval Jha

Reputation: 358

I also don't find why your code is not working...
But i have find a solution to relate a product to Order Id.

Try this code to assign the product to Order Id.

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

$product->save(); 

Upvotes: 1

Related Questions