Problem/motivation

Orders have a total_price that is saved after being calculated from order items and adjustments, but sometimes we need access to the order subtotal and individual adjustments as well, e.g in the case of displaying the cart summary with line items for taxes, shipping, promotions, etc.

Proposed resolution

  • Create a new service that provides a buildTotals() method for summarizing an order's subtotal and total amounts as well as groups and orders the order adjustments.
  • Use that service to display not just the order total price, but the order total summary, in places like the checkout summary block, order views, and order receipt templates

Completed tasks

  1. Created new 'commerce_order.order_total_summary' interface and service with a public buildTotals method that returns an array of useful summary properties for an order:
    • 'subtotal' (amount)
    • 'adjustments' (type-grouped and source_id-combined adjustments ordered by weight)
    • 'total' (amount)
  2. Created a new theme hook that uses that service to produce a two-column summary.
  3. Created a new OrderTotalSummary field formatter that uses that template.
  4. Used the field formatter in the Checkout order summary block footer:

  5. Used the totals in the order receipt email template:

  6. Set the Order total summary formatter as the default for Order displays:

Remaining tasks

  1. Fix failing test assertions or move those to follow-up

See https://github.com/drupalcommerce/commerce/pull/594.

Comments

steveoliver created an issue. See original summary.

bojanz’s picture

Title: Display adjustments in cart block » Implement the order summary totals

We need an OrderSummary service that will build the totals and supply them to the order total formatter for display.
The OrderSummary needs to inject the adjustment type manager to get the relevant weights.
Then in the buildTotals($order) method it calculates the subtotals the adjustment totals, the end total, returns all of that.

We summarize adjustments by adding together all of them which have the same type and source id. If the source id is NULL, we always leave that adjustment alone. Ideally we'd have $order->collectAdjustments() that gave you an array of all adjustments (order item + order ones).

(I guess the logic could live in the formatter itself but in our initial discussion having a service seemed cleaner.

steveoliver’s picture

Status: Active » Needs review
StatusFileSize
new24.82 KB
bojanz’s picture

Status: Needs review » Needs work

We're close on this one. Primary tasks remaining are a unit test for the service and styling for the totals (something with two-columns and minimal styling).

ransomweaver’s picture

@steveoliver, I'm testing and I have a problem. I add a tax using an order process service, like this: https://github.com/mglaman/commerce_demo/blob/master/src/OrderProcessor/.... Before patching with your code, this works correctly, the displayed order amount in checkout is adjusted and saved with the order, the adjustment is blobbed in the db for the order. After, my tax order process runs before your order total summary (i put a dsm in each), but $collected_adjustments = $order->collectAdjustments(); is empty, and in the db the order amount isn't adjusted. However, the adjustment, with the correct adjustment amount, DOES appear in commerce_order__adjustments. Any insight on this?

steveoliver’s picture

Assigned: steveoliver » Unassigned
Status: Needs work » Needs review

I'm not sure why, but I notice the following two issues outstanding that are blocking this issue:

1. nothing but 'custom' adjustment types show up in the OrderTotalSummary::buildTotals 'adjustments' element.
2. however, all adjustments are included in the total calculation
3. except ... nothing but 'custom' adjustment types are included in the order total calculation.

I'm unassigning myself in the hopes that mglaman, bojanz or someone else picks the issue up and finds out what I'm doing wrong.

@ransomweaver - I'm not sure why you're having that issue. Are you defining the adjustment type before trying to create an adjustment of that type? See (edit for development) commerce_order.commerce_adjustment_types.yml.

steveoliver’s picture

Issue summary: View changes

We should also include the order total summaries in 1. the order admin view and 2. the order receipt.

steveoliver’s picture

Adding related issue.

steveoliver’s picture

ransomweaver’s picture

@steveoliver You are correct, I didn't define a "tax" adjustment type in a module yml file, to match the type I was creating in the OrderProcessor. Now that I have done that, it works. Thanks!

steveoliver’s picture

Issue summary: View changes
StatusFileSize
new140.5 KB
new124.23 KB
new141.66 KB
steveoliver’s picture

Issue summary: View changes
steveoliver’s picture

Issue summary: View changes

Update issue summary.

bojanz’s picture

Assigned: Unassigned » bojanz

Wrapping this up.

bojanz’s picture

  • bojanz committed ea1ce2a on 8.x-2.x authored by steveoliver
    Issue #2840864 by steveoliver, bojanz: Implement the order summary...
bojanz’s picture

Status: Needs review » Fixed

Boom!

ransomweaver’s picture

The change from using @steveoliver's pull request as a patch to this commit in the lastest 2.x-dev has caused the rendering of my Tax order adjustment to break.

It throws an error in commerce/modules/price/src/TwigExtension/PriceTwigExtension.php, InvalidArgumentException, because param price is null.

However, the summary DOES total the price with tax correctly (if I insert "return null" before throw new \InvalidArgumentException so I can see the page load).

Subtotal $1,815.00
Sales tax << where the problem is
Total $1,964.74

bojanz’s picture

@ransomweaver
One of my changes was tweaking the output format of the totals.
You might have a custom template that's still using the old structure / variable names?

steveoliver’s picture

Status: Fixed » Needs work
StatusFileSize
new861 bytes

Nope, @ransomweaver is right -- adjusments have adjustment.amount, not adustment.total -- we need to apply this patch.

steveoliver’s picture

StatusFileSize
new1.42 KB

...this one fixes issues with both the theme callback template and the order email template.

ransomweaver’s picture

#21 works great, thanks @steveoliver

  • bojanz committed 8396ed1 on 8.x-2.x
    Issue #2840864 followup: Fix the OrderTotalSummary key name (amount ->...
bojanz’s picture

Status: Needs work » Fixed

I wanted to rename amount to total, but the rename was incomplete. Now fixed. Sorry for the disturbance.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.