Adding Payment Method to Drupal Commerce Orders View (No Duplicates!)

Thu, 02/26/2015 - 12:56 -- admin

Today I'm going to show you how to add the Payment Method of an order to the default Orders view that Drupal Commerce creates (located at [YOUR DOMAIN]/admin/commerce/orders).

The module you will need besides Drupal Commerce, Drupal Commerce Orders, and Drupal Commerce Payment is:


Block creation

After downloading and enabling views_field_view, open the default Orders view that is created by Drupal Commerce.  I was using Commerce VBO Views in this example, because I was working on a large site (I highly recommend Commerce VBO Views for bigger sites) -- so my Orders (Commerce Order) view was located at [YOUR DOMAIN]/admin/structure/views/view/commerce_vbo_views_orders.

Once you have the View open, you need to add a new display of the type "Block".  In that new block display, you need to add a contextual filter of the Order ID (with no relationship for the contextual filter).  I chose to display the contents of "No results found" in the case that no contextual filter is present (although this should never occur).




Next you will need to add a relationship to the view.  Select the Commerce Order: Payment Transaction relationship, and configure it as below:



After adding the relationship, add a Field for Commerce Payment Transaction: Payment method.  We will select the Transaction Payment relationship that we just set up, with no label, and displaying "The full administrative title of the payment method".



Finally, we will limit the amount of results in the view to 1 under the Pager section.  I've also set the Format of this view to be "Unformatted list", as this view is just needs to be returning one text field.




Commerce Order -- Adding a View within a View

Now let's add our new block as a field, within the default Commerce Order view.  Navigate back to the "Admin page" display, and add a field of the "Global: View" type.  The settings for this view are as follows:

  • Create a label of "Payment Method"
  • Under VIEW SETTINGS, select the "Orders" view (the view we are currently editing).  For "Display", choose the block display we created in the first steps of this guide.  Then for the "Contextual Filter", you will want to use [!order_number] (which is a replacement pattern for the order number).

Go ahead and apply & save the view, and everything should be working!  The payment method for each order should be listed as an item in the [YOUR DOMAIN]/admin/commerce/orders table.  There should be no duplicate orders listed, since we are limiting the results to 1 in the block display of the view.  There might be other ways to achieve this, but using a view within a view was the most sound method I found.

Let me know if you have any questions, in the comments section below!  Thanks!