I am using Drupal Commerce Kickstart! I want to get 'Only' the country name from the shipping information of a particular order in a view, The shipping information field in my view shows the entire shipping address including the country at last, is there a way that I can extract only the country name from that? or a db query using the order_id?
3 Answers
Implying that you have the Order ID in your view:
- Add a relationship to Commerce Order: Referenced customer profile, for the shipping address (Relate this entity to the customer profile referenced by its commerce_customer_shipping value)
- Add field for Commerce Customer profile: Address - Country
My output after those steps:
Order ID: 4
Bulgaria
Order ID: 5
United States
FYI the data is located in field_data_commerce_customer_address table commerce_customer_address_country column.
-
Hello Victor, Thanks for the response. But this didn't work either, the country failed to display but the full address did. I wrote a db query to get the results out of it. Ill right it down. Commented Jan 28, 2014 at 6:53
-
Hey DropDragon, are you sure that you select the right data field? I'm positive that this Views set up imgur.com/yoSzvNG displays the output in my answer. Commented Jan 30, 2014 at 8:51
-
Yes I am sure I did it. but as posted before this doesn't show me the country name, but the replacement pattern works only for the full address Thank you. Commented Feb 7, 2014 at 12:35
Finally I did it by sending a DB query to get the address of a customer by an order id.
SELECT field_data_commerce_customer_address.commerce_customer_address_country as country
FROM field_data_commerce_customer_address
INNER JOIN field_data_commerce_customer_shipping
ON field_data_commerce_customer_address.entity_id = field_data_commerce_customer_shipping.commerce_customer_shipping_profile_id
WHERE field_data_commerce_customer_shipping.entity_id ='".$order_id."'"
You can isolate the country field by enabling aggregation. Add address to your result set and group the results by the country field.