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 = Drupalcommerce_orderEntityOrderItem::create([ 'type' => 'default', 'purchased_entity' => $produit, 'quantity' => $quantite, ]); $unit_price = new Drupalcommerce_pricePrice($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(); } }