首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何与商业自动重复订货2

如何与商业自动重复订货2
EN

Drupal用户
提问于 2018-01-25 09:25:49
回答 1查看 265关注 0票数 0

如何自动复制订单?我使用Drupal 8和Drupal 2。我需要复制一些订单,每周,自动。

订单有一个布尔字段(recurrent_order)。当字段被填充时,网站必须在周末创建相同的订单。

我在Drupal 7上使用了rules和VBO实现了这一点:在维护任务中,规则找到一个由VBO视图填充的数组,并且一个组件复制内容。

在Drupal 8中,不存在来自规则的VBO视图访问。

我怎样才能做到这一点?致以敬意,

EN

回答 1

Drupal用户

发布于 2018-07-09 20:33:16

下面是cron作业重复订单的代码:

代码语言:javascript
复制
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();
   }        
}
票数 0
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/254454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档