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);

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

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

    $nid= $abo->id();

    $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,


    //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->set('order_number', $order->id());        
    $order->set('state', 'complete');

    //prix custo
    foreach ($order->getItems() as $ligne_de_commande) {

        $produit = $ligne_de_commande->getPurchasedEntity();
        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');




Order refresh is done via a service, this service is called every time an order is saved. Check the module documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.