簡體   English   中英

在WooCommerce中添加自定義URL鏈接到管理員訂單列表頁面

[英]Add custom URL link to admin order list page in WooCommerce

嗨,我正在嘗試添加AfterShip跟蹤按鈕或鏈接到后端的管理員訂單列表。 我成功創建了一個新列,顯示每個訂單的跟蹤編號。 但是,我想使跟蹤號碼可以點擊。 或者,作為替代方案,創建一個操作按鈕,用於打開新選項卡並跟蹤“跟蹤編號”列中的編號。

我需要的URL格式如下: 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上得到了這段代碼..真棒資源。

將自定義列添加到WooCommerce后端的管理訂單列表中

您可以提供任何幫助或建議,將不勝感激。 提前致謝!

WC 3.3+的新更新: 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文件中,或者放在任何插件文件中。

經過測試和工作。 你會得到類似的東西:

在此輸入圖像描述


現在,要使您的跟蹤號碼可以點擊,您將在代碼中替換此功能:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM