在WooCommerce中添加自定义URL链接到管理员订单列表页面

时间:2017-12-19 11:29:35

标签: php wordpress woocommerce custom-fields hook-woocommerce

您好我正在尝试添加AfterShip跟踪按钮或链接到后端的管理员订单列表。我成功创建了一个新列,显示每个订单的跟踪编号。但是,我想使跟踪号码可以点击。或者,作为替代方案,创建一个操作按钮,用于打开新选项卡并跟踪“跟踪编号”列中的编号。

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

请注意跟踪号码后面附有问号。我需要对动作执行此操作,因为输入跟踪号时不使用问号符号。

以下是我用于在后端的管理订单列表中显示跟踪编号列的代码:

//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上得到了这段代码..很棒的资源。

Add custom columns to admin orders list in WooCommerce backend

您可以提供任何帮助或建议,我们将不胜感激。提前谢谢!

1 个答案:

答案 0 :(得分:2)

  

WC 3.3 +的新更新: Custom action button in admin orders list on Woocommerce 3.3+

以下是在管理员订单列表中添加操作按钮的方法,其中包含与跟踪相关的自定义链接(在新窗口中打开链接 ):< / p>

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