0

In my module, on a cron_job function I need to create an order with specific order_item price. Here is my code :

        switch ($role[1]) {
            case 'biocoop':
                $role_price = $produit->get('field_prix_biocoop');
                break;
            case 'montagne':
                $role_price = $produit->get('field_prix_montagne');

                break;
            case 'vente_directe':
                $role_price = $produit->get('field_prix_vd');
                break;
              /* default:
              $role_price = $produit->getPrice();
              break; */
        }     

        $order_item = \Drupal\commerce_order\Entity\OrderItem::create([
            'type' => 'default',
            'purchased_entity' => $produit,
            'quantity' => $quantite,
        ]);
        $unit_price = new \Drupal\commerce_price\Price($role_price->number, $role_price->currency_code);
        $order_item->setUnitPrice($unit_price);
        $order_item->save();
        \Drupal::logger('commande_et_paiement')->notice('Prix @role : @prix',
        array(
            '@prix' => $order_item->getUnitPrice(),
            '@role' => $role[1],

        ));  

        $line_item[$i]=$order_item;
    }

    //creation de la commande
    $entity_type_manager = \Drupal::service('entity_type.manager');
    $order_storage = $entity_type_manager->getStorage('commerce_order');
    $new_order = Order::create([
      'type' => 'default',
      'store_id' => 1,
      'checkout_flow' => 'boulangerie',      
      'uid' => $user,
      'billing_profile' =>$profile,
      'order_items' =>$line_item,
      //'state' => 'draft',  
      'field_abonnement' => $nid,
      'placed' => time(),
      'field_date_livraison' =>$date_livraison->format('Y-m-d'),

    ]);

    $new_order->save();
    $new_order->set('order_number', $new_order->id());
    $new_order->save();

}

In the watchdog the price is good, but not in my order. If I try to set quantity of the order_item just after the setPrice function, the quantity is OK in my order too :

// Set the quantity with setQuantity.
    $order_item->setQuantity(50);

It seems that when the order is saved the order_item price has been updated?

Here is my resolver class :

class PrixRoleResolver implements PriceResolverInterface {
    public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {
        //kint($context);
        $current_user_id = \Drupal::currentUser()->id();
        $account = User::load($current_user_id);

        if ($account->hasRole('biocoop')) {
            $role_price = $entity->get('field_prix_biocoop');
            return new Price($role_price->number, $role_price->currency_code);
        }

        if ($account->hasRole('montagne')) {
            $role_price = $entity->get('field_prix_montagne');
            return new Price($role_price->number, $role_price->currency_code);
        }

        if ($account->hasRole('vente_directe')) {
            $role_price = $entity->get('field_prix_vd');
            return new Price($role_price->number, $role_price->currency_code);
        }

        if ($account->hasRole('administrateur_boutique')) {
            //l'administrateur quand il créé une commande le site applique le prix lié au role du client

            $client=$context->getCustomer();
            $client_uid = $client->uid;
            if (!empty($client_uid))  { 
                $uid = $client_uid->getString();
                //kint($uid); 
                if (!empty($uid)) {
                    $client_user_id = $uid;
               ÷    $account = User::load($client_user_id);
                    if ($account->hasRole('biocoop')) {
                        $role_price = $entity->get('field_prix_biocoop'); 
                        return new Price($role_price->number, $role_price->currency_code);
                    }
                }
            }

        }


        return $entity->getPrice();
    }
}
0

Yes, the price of order items in unplaced orders (i.e. carts) is recalculated regularly in Drupal 8 similar to the product pricing process in Drupal 7. If you're trying to achieve a different base price for different user roles, you likely need to implement a custom price resolver to do so.

Unfortunately, we currently have documentation handy for that, but you can look at our work in the Drupal 8 version of Commerce Pricelist for an example. (This is actually one of the key features of that module, so perhaps you can eventually eliminate your custom code.)

  • Thank's Ryan, I have already a custom price resolver, that's fine to display good price for customer according to the user role, but when I create programmatically an order on cron_job, the function is not called... I try the Commerce Pricelist, but the latest release date is june 2015... The module seems to have lot of bugs, can not add reference fields.... – christophe Natale Jul 6 '18 at 9:41
  • I just added the class resolver to my question. – christophe Natale Jul 6 '18 at 9:47

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.