I am using WooCommerce plugin for one of my ecommerce WordPress websites. I want to add some columns to my order listing page in the WooCommerce admin area. I am not able to find out where to add that.
Can anyone advise which template page I need to amend in order to meet my requirement?
Updated: 2018-03-30 - added positioning feature to the new columns
So you if you want to add some columns in the orders Admin list page (in backend):
ADDING COLUMNS IN WOOCOMMERCE ADMIN ORDERS LIST
In the example below, we add 2 new custom columns, before existing "Total" and "Actions" columns.
// ADDING 2 NEW COLUMNS WITH THEIR TITLES (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($columns)
{
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if( $key == 'order_status' ){
// Inserting after "Status" column
$reordered_columns['my-column1'] = __( 'Title1','theme_domain');
$reordered_columns['my-column2'] = __( 'Title2','theme_domain');
}
}
return $reordered_columns;
}
// Adding custom fields meta data for each new column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
switch ( $column )
{
case 'my-column1' :
// Get custom post meta data
$my_var_one = get_post_meta( $post_id, '_the_meta_key1', true );
if(!empty($my_var_one))
echo $my_var_one;
// Testing (to be removed) - Empty value case
else
echo '<small>(<em>no value</em>)</small>';
break;
case 'my-column2' :
// Get custom post meta data
$my_var_two = get_post_meta( $post_id, '_the_meta_key2', true );
if(!empty($my_var_two))
echo $my_var_two;
// Testing (to be removed) - Empty value case
else
echo '<small>(<em>no value</em>)</small>';
break;
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related answer (for products): Add custom columns to admin producs list in WooCommerce backend
Related
Based on "Display back Order Notes in Admin orders list on WooCommerce 3.3" answer code, that adds in a custom column the order notes in a tooltip, I am trying to figure out how to get the correct data from the "Order Notes" where it shows the transaction ID to display it in a custom column in admin orders list.
Because what this linked answer code is displaying a little icon in a column that when you hover over it shows you the last order note.
All i'm wanting to populate in the column is the transaction ID from the order note.
Updated - You can simply display the transaction ID using the WC_Order get_transaction_id() method like:
echo $the_order->get_transaction_id();
So your code hooked functions will be:
// Add a custom column before "actions" last column
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 100 );
function custom_shop_order_column( $columns ){
$ordered_columns = array();
foreach( $columns as $key => $column ){
$ordered_columns[$key] = $column;
if( 'order_date' == $key ){
$ordered_columns['transaction_id'] = __( 'Transaction id', 'woocommerce');
}
}
return $ordered_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'custom_shop_order_list_column_content', 10, 1 );
function custom_shop_order_list_column_content( $column ) {
global $post, $the_order;
if ( 'transaction_id' === $column ) {
echo $the_order->get_transaction_id();
}
}
Code goes in function.php file of your active child theme (or active theme). It should work.
I was trying to add new column to the orders page of the customer's recent orders table, the new column should show the details of the product like product name & description.
Tried different solution, was fortunate to add column name but not able to add the value to the column.
Here is my code:
add_filter( 'woocommerce_account_orders_columns', 'new_orders_columns' );
function new_orders_columns( $columns = array() ) {
// Hide the columns
if( isset($columns['order-total']) ) {
unset( $columns['order-status'] );
unset( $columns['order-total'] );
unset( $columns['order-actions'] );
}
// Add new columns
//this is my custom column order details
$columns['order-detail'] = __( 'Details', 'woocommerce' );
$columns['order-status'] = __( 'Status', 'woocommerce' );
$columns['order-total'] = __( 'Total', 'woocommerce' );
$columns['order-actions'] = __( ' ');
return $columns;
}
Screenshot of the result:
You are very near…
1) Insert a new column in a specific location (another way):
add_filter( 'woocommerce_account_orders_columns', 'add_custom_account_orders_column', 10, 1 );
function add_custom_account_orders_column( $columns ) {
$ordered_columns = array();
// Inserting a new column in a specific location
$ordered_columns['order-number'] = $columns['order-number'];
$ordered_columns['order-date'] = $columns['order-date'];
$ordered_columns['order-details'] = __( 'Details', 'woocommerce' ); // <== New column
$ordered_columns['order-status'] = $columns['order-status'];
$ordered_columns['order-total'] = $columns['order-total'];
$ordered_columns['order-actions'] = $columns['order-actions'];
return $ordered_columns;
}
Code goes in function.php file of your active child theme (or active theme).
tested and works.
2) Display data in this new column (editing myaccount/orders.php template):
You will need to override through your active child theme, the myaccount/orders.php WooCommerce template.
At line 56 (just after 'order-number' code) you will insert the following and you will be able to add some specific content:
<?php elseif ( 'order-details' === $column_id ) : ?>
<?php
// HERE GOES YOUR CUSTOM CODE (DISPLAYED CONTENT)
?>
You can use the available WC_Order object $order…
How to get WooCommerce order details
Accepted answer is only partially correct. Adding the column via filter is fine, but populating the column is better done via a action hook. This is clean and matches best with column being added via filter. While editing the orders.php page via child theme works, it will lead to future effort to maintain after updates to woocommerce change this page making child theme version out of date.
To add your content to the column create an action hook along side the filter in functions.php. (or better yet in a plugin!)
add_action( 'woocommerce_my_account_my_orders_column_order-detail' , 'your_function_name2',10,1 );
//'woocommerce_my_account_my_orders_column_<column_id>'
function your_function_name2( $order ) {
// do stuff, ex: get_post_meta( $post->ID, 'key', true );
}
add_filter( 'woocommerce_account_orders_columns', 'new_orders_columns',10,1 );
function new_orders_columns( $columns) {
// Add new column. Kept simple to play nice with other filters.
$columns['order-detail'] = __( 'Details', 'woocommerce' );
return $columns;
}
see this similar question, best answer by Misha
Try to use this code
<?php
// ------------------
// 1. Register new endpoint to use for My Account page
// Note: Resave Permalinks or it will give 404 error
function bbloomer_add_premium_support_endpoint() {
add_rewrite_endpoint( 'premium-support', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'bbloomer_add_premium_support_endpoint' );
// ------------------
// 2. Add new query var
function bbloomer_premium_support_query_vars( $vars ) {
$vars[] = 'premium-support';
return $vars;
}
add_filter( 'query_vars', 'bbloomer_premium_support_query_vars', 0 );
// ------------------
// 3. Insert the new endpoint into the My Account menu
function bbloomer_add_premium_support_link_my_account( $items ) {
$items['premium-support'] = 'Premium Support';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'bbloomer_add_premium_support_link_my_account' );
// ------------------
// 4. Add content to the new endpoint
function bbloomer_premium_support_content() {
echo '<h3>Premium WooCommerce Support</h3><p>Welcome to the WooCommerce support area. As a premium customer, you can submit a ticket should you have any WooCommerce issues with your website, snippets or customization. <i>Please contact your theme/plugin developer for theme/plugin-related support.</i></p>';
echo do_shortcode( ' /* your shortcode here */ ' );
}
add_action( 'woocommerce_account_premium-support_endpoint', 'bbloomer_premium_support_content' );
Hi I am trying to add an AfterShip tracking button or link to my Admin Order list in the backend. I have succesfully created a new column that displays the tracking number for each order. However, I would like to make the tracking number clickable. Or, as an alternative, create an action button that opens a new tab and tracks the number in the Tracking Number column.
The URL format I need is as follows:
https://track.aftership.com/LS325245095CN?
Notice that there is a question mark appended to the tracking number. I would need to do this with the action, since the question mark symbol is not used when entering the tracking number.
Here is the code I am using for displaying the tracking number column in the admin orders list in the backend:
//Start Add Tracking Number to Admin Orders List
//Start Add Header to List
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',
12, 1 );
function custom_shop_order_column($columns)
{
// Set "Actions" column after the new colum
$action_column = $columns['order_actions']; // Set the title in a variable
unset($columns['order_actions']); // remove "Actions" column
//add the new column "New Tracking Number"
$columns['order_astracking'] = '<span>'.__( 'Tracking Number','woocommerce').'</span>'; // title
// Set back "Actions" column
$columns['order_actions'] = $action_column;
return $columns;
}
//END Add Header to List
//START Add Tracking Number Data to List
add_action( 'manage_shop_order_posts_custom_column' ,
'custom_order_list_column_content', 10, 2 );
function custom_order_list_column_content( $column, $post_id )
{
// HERE get the data from your custom field (set the correct meta key below)
$astracking = get_post_meta( $post_id, '_aftership_tracking_number', true );
if( empty($astracking)) $astracking = '';
switch ( $column )
{
case 'order_astracking' :
echo '<span>'.$astracking.'</span>'; // display the data
break;
}
}
//END Add Tracking Number Data to List
//START Make Tracking Number Data Searchable in Admin Orders List
add_filter( 'woocommerce_shop_order_search_fields',
'astracking_search_fields', 10, 1 );
function astracking_search_fields( $meta_keys ){
$meta_keys[] = '_aftership_tracking_number';
return $meta_keys;
}
//END Make Tracking Number Data Searchable in Admin Orders List
//END Add Tracking Number to Admin Orders List
I got this code here on Stackoverflow.. awesome resource.
Add custom columns to admin orders list in WooCommerce backend
Any help or suggestions you could provide, would be greatly appreciated. Thanks in advance!
New Update for WC 3.3+: Custom action button in admin orders list on Woocommerce 3.3+
Here is the way to add an action button in admin order list with a custom link related to tracking (opening the link in a new window as requested):
// Add your custom order action button
add_action( 'woocommerce_admin_order_actions_end', 'add_custom_order_actions_button', 100, 1 );
function add_custom_order_actions_button( $order ) {
// Get the tracking number
$traking_number = get_post_meta( $order->get_id(), '_aftership_tracking_number', true );
if( empty($traking_number) ) return;
// Prepare the button data
$url = esc_url('https://track.aftership.com/'.$traking_number.'?');
$name = esc_attr( __('Tracking', 'woocommerce' ) );
$action = esc_attr( 'view tracking' ); // keep "view" class for a clean button CSS
// Set the action button
printf( '<a class="button tips %s" href="%s" data-tip="%s" target="_blank">%s</a>', $action, $url, $name, $name );
}
// The icon of your action button (CSS)
add_action( 'admin_head', 'add_custom_order_actions_button_css' );
function add_custom_order_actions_button_css() {
echo '<style>.view.tracking::after { font-family: woocommerce; content: "\e005" !important; }</style>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. You will get something like:
Now To make your tracking number clickable you will replace this function in your code:
add_action( 'manage_shop_order_posts_custom_column', 'custom_order_list_column_content', 10, 2 );
function custom_order_list_column_content( $column, $post_id )
{
// HERE get the data from your custom field (set the correct meta key below)
$astracking = get_post_meta( $post_id, '_aftership_tracking_number', true );
if( empty($astracking)) $astracking = '';
switch ( $column )
{
case 'order_astracking' :
echo '<span>'.$astracking . '</span>'; // display the data
break;
}
}
On woocommerce, I am using the code below to render some product custom fields, on cart and on checkout:
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['wccpf_enter_product_id'] ) ) {
$diamond = $cart_item['wccpf_enter_product_id'];
$pacolor = get_the_terms($diamond, 'pa_color');
foreach ( $pacolor as $pacolor ) {
$color= $pacolor ->name;
}
$custom_items[] = array( "name" => "color", "value" => $color);
}
return $custom_items;
}
How can I display that custom product fields wccpf_enter_product_id' value in orders?
Thanks.
You can use a custom function hooked in woocommerce_add_order_item_meta action hook to achieve this.
You will need first to add an attribute in your products to get a "readable clean label" for your custom field value that is going to appear as order items meta data.
For that you have to create an attribute first and then set it in your product with any value (as you will replace this value in the code below).
See HERE some more explanations about that process…
You will have to replace in my code the 'custom_field_key' by your specific custom key that you will find on wp_woocommerce_order_itemmeta MySQL table for the corresponding item ID for your specific Order ID.
To find the corresponding item ID for the order, you can search in wp_woocommerce_order_items MySQL table with the Order ID…
You will have also to set your correct attribute slug instead of 'pa_your_attribute' to display in your orders the correct label text for this custom field value.
(see below other similar answers references).
So your code will be something like this:
// ADD THE INFORMATION AS META DATA SO THAT IT CAN BE SEEN AS PART OF THE ORDER
add_action('woocommerce_add_order_item_meta','add_and_update_values_to_order_item_meta', 1, 3 );
function add_and_update_values_to_order_item_meta( $item_id, $item_values, $item_key ) {
// Getting your custom product ID value from order item meta
$custom_value = wc_get_order_item_meta( $item_id, 'custom_field_key', true );
// Here you update the attribute value set in your simple product
wc_update_order_item_meta( $item_id, 'pa_your_attribute', $custom_value );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work
Related answers:
Adding user custom field value to order items details
Add custom Product data dynamically as item meta data on the Order
Displaying custom product data in Order items view
I have created a custom column "Cost" in Backend WooCommerce Products list panel using a hook in functions.php file.
I want to move that column after "Price" column:
How can I achieve this?
There is many ways to achieve this. I have chosen the simplest way to do it. You can use the code snippet below to change "Cost" column location before "price" column, in product post_type backend list:
// ADDING A CUSTOM COLUMN TITLE TO ADMIN PRODUCTS LIST AFTER "Price" COLUMN
add_filter( 'manage_edit-product_columns', 'custom_product_column',11);
function custom_product_column($columns)
{
$new_columns = array();
foreach( $columns as $key => $column ){
$new_columns[$key] = $columns[$key];
if( $key === 'price' )
$new_columns['cost'] = __( 'Cost','woocommerce');
}
return $new_columns;
}
// ADDING THE DATA FOR EACH PRODUCTS BY COLUMN (EXAMPLE)
add_action( 'manage_product_posts_custom_column' , 'custom_product_list_column_content', 10, 2 );
function custom_product_list_column_content( $column, $product_id )
{
global $post;
switch ( $column )
{
case 'cost' :
// Get and display the value for each row
echo get_post_meta( $product_id, '_cost', true );
break;
}
}
Tested and works.
Use the add_filter(manage_edit-posttype_columns, yourFunction) where posttype is your post type. Then list your columns in yourFunction the order you want. The best tutorial I've seen on this is here.