@@ -144,21 +144,9 @@ public function isUnitPriceOverridden() {
144144 /**
145145 * {@inheritdoc}
146146 */
147- public function getAdjustedUnitPrice (array $ adjustment_types = []) {
148- if ($ unit_price = $ this ->getUnitPrice ()) {
149- $ adjusted_price = $ unit_price ;
150- foreach ($ this ->getAdjustments () as $ adjustment ) {
151- if ($ adjustment_types && !in_array ($ adjustment ->getType (), $ adjustment_types )) {
152- continue ;
153- }
154- if ($ adjustment ->isIncluded ()) {
155- continue ;
156- }
157-
158- $ adjusted_price = $ adjusted_price ->add ($ adjustment ->getAmount ());
159- }
160-
161- return $ adjusted_price ;
147+ public function getTotalPrice () {
148+ if (!$ this ->get ('total_price ' )->isEmpty ()) {
149+ return $ this ->get ('total_price ' )->first ()->toPrice ();
162150 }
163151 }
164152
@@ -196,22 +184,82 @@ public function removeAdjustment(Adjustment $adjustment) {
196184 /**
197185 * {@inheritdoc}
198186 */
199- public function getTotalPrice () {
200- if (!$ this ->get ('total_price ' )->isEmpty ()) {
201- return $ this ->get ('total_price ' )->first ()->toPrice ();
202- }
187+ public function usesLegacyAdjustments () {
188+ return $ this ->get ('uses_legacy_adjustments ' )->value ;
203189 }
204190
205191 /**
206192 * {@inheritdoc}
207193 */
208194 public function getAdjustedTotalPrice (array $ adjustment_types = []) {
209- if ($ adjusted_unit_price = $ this ->getAdjustedUnitPrice ($ adjustment_types )) {
210- $ rounder = \Drupal::service ('commerce_price.rounder ' );
195+ $ total_price = $ this ->getTotalPrice ();
196+ if (!$ total_price ) {
197+ return NULL ;
198+ }
199+
200+ if ($ this ->usesLegacyAdjustments ()) {
201+ $ adjusted_unit_price = $ this ->getAdjustedUnitPrice ($ adjustment_types );
211202 $ adjusted_total_price = $ adjusted_unit_price ->multiply ($ this ->getQuantity ());
212- $ adjusted_total_price = $ rounder ->round ($ adjusted_total_price );
213- return $ adjusted_total_price ;
214203 }
204+ else {
205+ $ adjusted_total_price = $ this ->applyAdjustments ($ total_price , $ adjustment_types );
206+ }
207+
208+ $ rounder = \Drupal::service ('commerce_price.rounder ' );
209+ $ adjusted_total_price = $ rounder ->round ($ adjusted_total_price );
210+
211+ return $ adjusted_total_price ;
212+ }
213+
214+ /**
215+ * {@inheritdoc}
216+ */
217+ public function getAdjustedUnitPrice (array $ adjustment_types = []) {
218+ $ unit_price = $ this ->getUnitPrice ();
219+ if (!$ unit_price ) {
220+ return NULL ;
221+ }
222+
223+ if ($ this ->usesLegacyAdjustments ()) {
224+ $ adjusted_unit_price = $ this ->applyAdjustments ($ unit_price , $ adjustment_types );
225+ }
226+ else {
227+ $ adjusted_total_price = $ this ->getAdjustedTotalPrice ($ adjustment_types );
228+ $ adjusted_unit_price = $ adjusted_total_price ->divide ($ this ->getQuantity ());
229+ }
230+
231+ $ rounder = \Drupal::service ('commerce_price.rounder ' );
232+ $ adjusted_unit_price = $ rounder ->round ($ adjusted_unit_price );
233+
234+ return $ adjusted_unit_price ;
235+ }
236+
237+ /**
238+ * Applies adjustments to the given price.
239+ *
240+ * @param \Drupal\commerce_price\Price $price
241+ * The price.
242+ * @param string[] $adjustment_types
243+ * The adjustment types to include in the adjusted price.
244+ * Examples: fee, promotion, tax. Defaults to all adjustment types.
245+ *
246+ * @return \Drupal\commerce_price\Price
247+ * The adjusted price.
248+ */
249+ protected function applyAdjustments (Price $ price , array $ adjustment_types = []) {
250+ $ adjusted_price = $ price ;
251+ foreach ($ this ->getAdjustments () as $ adjustment ) {
252+ if ($ adjustment_types && !in_array ($ adjustment ->getType (), $ adjustment_types )) {
253+ continue ;
254+ }
255+ if ($ adjustment ->isIncluded ()) {
256+ continue ;
257+ }
258+
259+ $ adjusted_price = $ adjusted_price ->add ($ adjustment ->getAmount ());
260+ }
261+
262+ return $ adjusted_price ;
215263 }
216264
217265 /**
@@ -337,19 +385,27 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
337385 ->setDescription (t ('Whether the unit price is overridden. ' ))
338386 ->setDefaultValue (FALSE );
339387
388+ $ fields ['total_price ' ] = BaseFieldDefinition::create ('commerce_price ' )
389+ ->setLabel (t ('Total price ' ))
390+ ->setDescription (t ('The total price of the order item. ' ))
391+ ->setReadOnly (TRUE )
392+ ->setDisplayConfigurable ('form ' , FALSE )
393+ ->setDisplayConfigurable ('view ' , TRUE );
394+
340395 $ fields ['adjustments ' ] = BaseFieldDefinition::create ('commerce_adjustment ' )
341396 ->setLabel (t ('Adjustments ' ))
342397 ->setRequired (FALSE )
343398 ->setCardinality (BaseFieldDefinition::CARDINALITY_UNLIMITED )
344399 ->setDisplayConfigurable ('form ' , FALSE )
345400 ->setDisplayConfigurable ('view ' , TRUE );
346401
347- $ fields ['total_price ' ] = BaseFieldDefinition::create ('commerce_price ' )
348- ->setLabel (t ('Total price ' ))
349- ->setDescription (t ('The total price of the order item. ' ))
350- ->setReadOnly (TRUE )
351- ->setDisplayConfigurable ('form ' , FALSE )
352- ->setDisplayConfigurable ('view ' , TRUE );
402+ $ fields ['uses_legacy_adjustments ' ] = BaseFieldDefinition::create ('boolean ' )
403+ ->setLabel (t ('Uses legacy adjustments ' ))
404+ ->setSettings ([
405+ 'on_label ' => t ('Yes ' ),
406+ 'off_label ' => t ('No ' ),
407+ ])
408+ ->setDefaultValue (FALSE );
353409
354410 $ fields ['data ' ] = BaseFieldDefinition::create ('map ' )
355411 ->setLabel (t ('Data ' ))
0 commit comments