【问题标题】:Ubercart: Create order with products programmatically Drupal 8Ubercart:以编程方式使用产品创建订单 Drupal 8
【发布时间】:2017-09-13 22:06:00
【问题描述】:

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

$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

【问题讨论】:

    标签: drupal drupal-8 ubercart


    【解决方案1】:

    我也找不到您的代码无法正常工作的原因...
    但我找到了将产品与订单 ID 相关联的解决方案。

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

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

    【讨论】:

    • 我无法在扩展 FormBase 类的自定义表单中以编程方式执行此操作。我们需要扩展或使用哪些类?
    猜你喜欢
    • 2019-10-14
    • 1970-01-01
    • 2015-12-09
    • 2012-05-18
    • 2013-05-05
    • 2016-03-19
    • 2014-08-11
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多