How duplicate order automatically with commerce 2
Here is the code for duplicate order with a cron job:

function commande_et_paiement_cron() {
$query = \Drupal::entityQuery('node');
$query-&gt;condition('status', 1);
$query-&gt;condition('type', 'abonnement');
$query-&gt;condition('field_livraison', 3);//livraison le vendredi
$entity_ids = $query-&gt;execute();


foreach ($entity_ids as $entity_id) {
$abo = Node::load($entity_id);

//utilisateur
$user = $abo-&gt;get('field_utilisateur')-&gt;getValue()[0]['target_id'];
$utilisateur = \Drupal\user\Entity\User::load($user);


//profile
$entity_manager = \Drupal::entityTypeManager();
$profile = $entity_manager-&gt;getStorage('profile')-&gt;load($user);  


//abonnement
$nid= $abo-&gt;id();

//livraison
$date_livraison = new DateTime();
$date_livraison-&gt;modify('next friday');



//ligne de commande
$line_items = array();
$abo_array= $abo-&gt;get('field_abo_produits')-&gt;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-&gt;getQuantity();
    $produit = $item-&gt;getPurchasedEntity();


    $order_item = \Drupal\commerce_order\Entity\OrderItem::create([
    'type' =&gt; 'default',
    'purchased_entity' =&gt; $produit,
    'quantity' =&gt; $quantite,
    //'unit_price' =&gt; $role_price,
    ]);

    $order_item-&gt;save();        
    array_push($line_items,$order_item);        
    }

//creation de la commande
$order = \Drupal\commerce_order\Entity\Order::create([
      'type' =&gt; 'default',
      'store_id' =&gt; 1,
      'checkout_flow' =&gt; 'boulangerie',      
      'uid' =&gt; $user,
      'billing_profile' =&gt;$profile,
      'field_abonnement' =&gt; $nid,
      'placed' =&gt; time(),
      'field_date_livraison' =&gt;$date_livraison-&gt;format('Y-m-d'),
      'order_items' =&gt;$line_items,
      'state' =&gt; 'draft'
    //'completed' =&gt; time(),

    ]);        
    $order-&gt;save(); 
    $order-&gt;set('order_number', $order-&gt;id()); 
    $order-&gt;save(); 
    $order-&gt;set('state', 'complete');
    /*$comment = 'blabla';
     $logStorage = \Drupal::entityTypeManager()-&gt;getStorage('commerce_log');        
    $logStorage-&gt;generate($order, 'order_comment', ['comment' =&gt; $comment])-&gt;save();      */
    $order-&gt;save();
   }        
}