Add a custom URL link to the admin order list page in WooCommerce

Hey. I am trying to add an AfterShip tracking button or a link to my list of administrators in the backend. I have successfully created a new column that displays the tracking number for each order. However, I would like to make the tracking number clickable. Or, alternatively, create an action button that opens a new tab and tracks the number in the Tracking Number column.

The URL format I need is this: https://track.aftership.com/LS325245095CN ?

Please note that a question mark has been added to the tracking number. I will need to do this with an action, since the question mark symbol is not used when entering the tracking number.

Here is the code I use to display the tracking number column in the list of admin orders 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 .. an amazing resource.

Add custom columns to list of admin orders in the WooCommerce backend

Any help or suggestions you could provide would be greatly appreciated. Thanks in advance!

+4
source share
1 answer

New update for WC 3.3 +: Custom action button in the list of admin orders in Woocommerce 3.3+

, ( ):

// 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>';
}

function.php ( ), .

. - :

enter image description here


, :

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;
    }
}
+2

Source: https://habr.com/ru/post/1691028/


All Articles