在WooCommerce中将自定义URL链接添加到管理订单列表页面 [英] Add custom URL link to admin order list page in WooCommerce

查看:114
本文介绍了在WooCommerce中将自定义URL链接添加到管理订单列表页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试添加一个AfterShip跟踪按钮或链接到后端的我的管理订单列表。我已经成功创建了一个新列,该列显示每个订单的跟踪号。但是,我想使跟踪号可点击。或者,作为一种替代方法,创建一个操作按钮,该按钮将打开一个新选项卡并在跟踪号列中跟踪该数字。

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.

我需要的URL格式如下:
https://track.aftership.com/LS325245095CN

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

我在Stackoverflow上获得了此代码。.很棒的资源。

I got this code here on Stackoverflow.. awesome resource.

在WooCommerce后端的管理订单列表中添加自定义列

您能提供的任何帮助或建议,将不胜感激。

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

推荐答案


WC 3.3+的新更新: 管理订单中的自定义操作按钮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>';
}

代码包含在您活动的子主题的function.php文件中(或主题)或任何插件文件中。

经过测试并可以使用。您将得到类似的内容:

Tested and works. You will get something like:

< img src = https://i.stack.imgur.com/RHR0B.png alt =在此处输入图片描述>

现在要使您的跟踪号码可点击,您将在代码中替换此函数:

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

这篇关于在WooCommerce中将自定义URL链接添加到管理订单列表页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆