I create orders programmatically, and I have a tax problem: the order, after changing the line item price, always has the tax cost with the old product price. How can I refresh the adjustment tax after changing the line-item price?
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
]);
$order->save();
$order->set('order_number', $order->id());
$order->save();
$order->set('state', 'complete');
$order->save();
//prix custo
foreach ($order->getItems() as $ligne_de_commande) {
$produit = $ligne_de_commande->getPurchasedEntity();
//prix
if ($utilisateur->hasRole('biocoop')) {
$role_price = $produit->get('field_prix_biocoop');
$role = 'biocoop';
}
if ($utilisateur->hasRole('montagne')) {
$role_price = $produit->get('field_prix_montagne');
$role = 'montagne';
}
if ($utilisateur->hasRole('vente_directe')) {
$role_price = $produit->get('field_prix_vd');
$role = 'vente_directe';
}
$unit_price = new \Drupal\commerce_price\Price($role_price->number, 'EUR');
$ligne_de_commande->setUnitPrice($unit_price);
$ligne_de_commande->save();
}
$order->save();
}