Sorry, if this isn't the correct place to post this question. But getting no responses on DrupalCommerce.org

Is it possible to display each individual component of the order total as it's own field in a view? Either by replacement pattern or rewrite output with php code to show that individual component for the order total?

I am setting up some views to show Sales Reports for the Commerce sales. Specifically using the views to export to a CSV file with all the order details. I have things working pretty good, however I'm struggling with showing the order total components (Subtotal, Shipping, Discounts, Tax, Order Total) as separate fields, instead of just the total.

I'm displaying the "Commerce Order: Order total". When I have the Formatter set to "Formatted amount" I get the full total as a $ amount. If I set it to "Formatted Amount with Components", it outputs a table with Subtotal, Shipping, Tax, Order Total (similar to what I see in the order view). My problem is that table doesn't export to CSV (it just leaves the column blank). but I'm trying to have each individual item show in it's own column.

Need something like "Commerce Order: Order Total: Tax" ??

I have played with the Commerce Reporting module but it isn't really outputting what I need. And the tax report is very limited.

Thanks for any help or direction anyone can provide.

Comments

rszrama’s picture

Status: Active » Closed (won't fix)

Nope, there's no way, though you should look at how the tax report works in http://drupal.org/project/commerce_reports. If you really need it to work the way you've described, then you'll need to define your own Views fields that essentially wrap the order total field handler to get the right data and then provide a different render function to extract just the price component you want.

tmsimont’s picture

Version: 7.x-1.3 » 7.x-1.x-dev
Status: Closed (won't fix) » Active

I think this would be a very important feature of any e-commerce back end reporting mechanism. If the receipt separates the shipping/tax/donation other components, why wouldn't views?

rszrama’s picture

Status: Active » Closed (won't fix)

My original comment probably wasn't clear enough - by no way, I mean that we literally cannot do this, not that I just didn't want to. Price component data is not stored in a way that makes it queryable by Views or direct SQL queries - it's sadly tucked away in the price field's data array. There are other issues in the queue that address the idea of running totals of these things, though, and there's the tax report in the module I've linked above that demonstrates the idea of batch processing orders to extract specific price components for reporting purposes.

tmsimont’s picture

Status: Closed (won't fix) » Active

For anyone else looking for the same feature, I was able to accomplish something like this with Views PHP

The code I used in the field code looks like this:

$order = commerce_order_load($row->order_number);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$currency_code = $wrapper->commerce_order_total->currency_code->value();
foreach ($wrapper->commerce_line_items as $line_item_wrapper) {
  if ($line_item_wrapper->type->value() == 'shipping') {
    // $line_item_wrapper->line_item_label->value();
   return commerce_currency_format($line_item_wrapper->commerce_unit_price->amount->value(), $currency_code);
  }
}

That would require an order number field, and shows the shipping item in its own field. Something similar could be used I'm sure for taxes or other order total components.

tmsimont’s picture

Status: Active » Closed (won't fix)

oops sorry didn't mean to override your status i think we posted at the same time.

rcharamella’s picture

Could you post some more details about how you accomplished this? I'm trying to do the same thing, but coming up empty. Any more specifics would be helpful.

alberto56’s picture

On admin/commerce/config/checkout/form/pane/checkout_completion_message, I am using replacement patterns to display the total amount. My client would like to display the tax information here, which is reasonable. If anyone has managed to do this, I'd love to hear from you!

TolstoyDotCom’s picture

Apparently to get the tax you use code like that in #4, but instead of checking for shipping you use this pseudocode:

$data_array = $line_item_wrapper->commerce_total->data->value();
foreach ( $data_array[ 'component' ] as $item ) {
  if ( $item[ 'name' ] contains 'tax|' ) {
    ...
  }
}

Then you add up the taxes. I don't know if there's a function to do that or how stable the array format is.

commerce_unit_price seems to have the same data as commerce_total.

The tax values have the name 'tax|new_york_state_tax' but I don't know if that's installation-specific. $data_array[ 'component' ] contains other things like discounts.

rszrama’s picture

There's actually a helper function for getting the total tax amount in the Tax module itself:

/**
 * Returns the total amount of tax included in a price components array.
 *
 * @param $components
 *   A price's components array including potential tax components.
 * @param $included
 *   Boolean indicating whether or not to include in the total taxes that were
 *   included in the price amount already.
 * @param $currency_code
 *   The currency to return the tax amount in.
 *
 * @return
 *   The consolidated tax collected for an order expressed as an integer amount.
 */
function commerce_tax_total_amount($components, $included, $currency_code) {
  // ...
}

Hope it helps!

j.wick’s picture

Issue summary: View changes

For non-developers:

Use the Views Field View module to imbed the relevant tax value on each line of a view table where each line represents one order. After enabling this module:

  1. Create a new view that shows "Commerce Tax Information".
  2. Add the field, "Commerce Tax Reports: Taxed" – you may want to remove the label
  3. Remove any default fields that you don't want appearing in your primary view
  4. Expand the Advanced column and add a contextual filter: Commerce Tax Reports: Order ID
  5. In the contextual filter settings, set: "When the filter value is not available" to Hide View
  6. Save this view

Now go into your the Orders view where you want to imbed the tax information:

  • If the view does not contain the Order ID field - add this field now (you can select "Exclude from display" to hide it from your view)
  • Add a new field: Global: View
  • Select the view you created above and the appropriate display (or leave the display set to Master)
  • Remember, you created a contextual filter so you must provide that value from the Order ID field. Enter the token (replacement pattern) - [!order_id]
  • Select Apply and Save your view.

Note: in order to use the token for Order ID, the Order ID field must be listed above the imbedded view field.

nicodv’s picture

jsimonis’s picture

So there's really no way to create a custom report in Views where you can show how much someone paid for shipping and how much they paid for taxes?

hockey2112’s picture

Does anyone have a method that works for Drupal 8? I am attempting to create a CSV export of order data, and the tax amount needs to be included.

The commerce_price_components mentioned in #11 does not have a D8 version, and the Views Field Views solution in #10 doesn't work because there iis no View type called "Commerce Tax Information".

jsimonis’s picture

I actually had to hire someone who created code for me to use in D7. This really should be standard since folks have to create tax reports.