如何自动复制订单?我使用Drupal 8和Drupal 2。我需要复制一些订单,每周,自动。
订单有一个布尔字段(recurrent_order)。当字段被填充时,网站必须在周末创建相同的订单。
我在Drupal 7上使用了rules和VBO实现了这一点:在维护任务中,规则找到一个由VBO视图填充的数组,并且一个组件复制内容。
在Drupal 8中,不存在来自规则的VBO视图访问。
我怎样才能做到这一点?致以敬意,
发布于 2018-07-09 20:33:16
下面是cron作业重复订单的代码:
function commande_et_paiement_cron() {
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'abonnement');
$query->condition('field_livraison', 3);//livraison le vendredi
$entity_ids = $query->execute();
foreach ($entity_ids as $entity_id) {
$abo = Node::load($entity_id);
//utilisateur
$user = $abo->get('field_utilisateur')->getValue()[0]['target_id'];
$utilisateur = \Drupal\user\Entity\User::load($user);
//profile
$entity_manager = \Drupal::entityTypeManager();
$profile = $entity_manager->getStorage('profile')->load($user);
//abonnement
$nid= $abo->id();
//livraison
$date_livraison = new DateTime();
$date_livraison->modify('next friday');
//ligne de commande
$line_items = array();
$abo_array= $abo->get('field_abo_produits')->getValue();
foreach ($abo_array as $abo_item) {
$ligne = $abo_item['target_id'];
//load produit abonnement
$item = \Drupal\commerce_order\Entity\OrderItem::load($ligne);
$quantite = $item->getQuantity();
$produit = $item->getPurchasedEntity();
$order_item = \Drupal\commerce_order\Entity\OrderItem::create([
'type' => 'default',
'purchased_entity' => $produit,
'quantity' => $quantite,
//'unit_price' => $role_price,
]);
$order_item->save();
array_push($line_items,$order_item);
}
//creation de la commande
$order = \Drupal\commerce_order\Entity\Order::create([
'type' => 'default',
'store_id' => 1,
'checkout_flow' => 'boulangerie',
'uid' => $user,
'billing_profile' =>$profile,
'field_abonnement' => $nid,
'placed' => time(),
'field_date_livraison' =>$date_livraison->format('Y-m-d'),
'order_items' =>$line_items,
'state' => 'draft'
//'completed' => time(),
]);
$order->save();
$order->set('order_number', $order->id());
$order->save();
$order->set('state', 'complete');
/*$comment = 'blabla';
$logStorage = \Drupal::entityTypeManager()->getStorage('commerce_log');
$logStorage->generate($order, 'order_comment', ['comment' => $comment])->save(); */
$order->save();
}
}https://drupal.stackexchange.com/questions/254454
复制相似问题