How can I refresh the adjusment tax after changing the line-item price?

06/07/2018


I create orders programmatically, I have a tax problem : the order, after changing the line item price , has always the tax cost with old product price .
How can I refresh the adjusment 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 = DrupaluserEntityUser::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 = Drupalcommerce_orderEntityOrderItem::load($ligne);
    $quantite = $item->getQuantity();
    $produit = $item->getPurchasedEntity();


    $order_item = Drupalcommerce_orderEntityOrderItem::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 = Drupalcommerce_orderEntityOrder::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  Drupalcommerce_pricePrice($role_price->number, 'EUR');

     $ligne_de_commande->setUnitPrice($unit_price);
     $ligne_de_commande->save();

    }

    $order->save();
}

قالب وردپرس