Add custom URL link to admin order list page in Wo

2020-07-18 04:59发布

问题:

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!

回答1:

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><a href="https://track.aftership.com/'.$astracking.'?" target="_blank">'.$astracking . '</a></span>'; // display the data
            break;
    }
}