簡體   English   中英

如何在“我的帳戶”頁面的“WooCommerce 我的訂單”列表中添加自定義數據作為新列?

[英]How to Add Custom Data as a New Column in the WooCommerce My Orders list in My Account page?

我正在嘗試將 1 列添加到 WooCommerce > 我的帳戶 > 訂單頁面。

使用 Jet-Engine,我在 WooCommerce 的客戶訂單中添加了一個名為“url-from-download”的字段。

里面有客戶訂單我select PDF。

下面的代碼添加了發票列和一個名為“下載”的文本,但它無法將文件提取為 url 供我下載。

我哪里弄錯了??

/**
 * Adds a new column to the "My Orders" table in the account.
 *
 * @param string[] $columns the columns in the orders table
 * @return string[] updated columns
 */
function th_wc_add_my_account_orders_column( $columns ) {

    $new_columns = array();

    foreach ( $columns as $key => $name ) {

        $new_columns[ $key ] = $name;

        // add ship-to after order status column
        if ( 'order-total' === $key ) {
            $new_columns['url-from-download'] = __( 'Invoice', 'textdomain' );
        }
    }

    return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'th_wc_add_my_account_orders_column' );

/**
 * Adds data to the custom "url-from-download" column in "My Account > Orders".
 *
 * @param \WC_Order $order the order object for the row
 */
function th_wc_my_orders_url_from_download_column( $order ) {

    $url_from_download = get_post_meta( $order->get_id(), 'url_from_download', true ); // Get custom order meta
    echo ! empty( $url_from_download ) ? $url_from_download : 'Download';
    
}
add_action( 'woocommerce_my_account_my_orders_column_url-from-download', 'th_wc_my_orders_url_from_download_column' ); 

我添加了一個名為“url-from-download”的字段

您有疑問地表示您的字段名為url-from-download但在您的代碼中,您使用的是url_from_download

破折號 (-) 和下划線 (_) 有所不同,因此請確保您在get_post_meta中使用了正確的元鍵

更新

根據討論,我假設你得到 url 但你需要它作為你可以點擊的按鈕樣式。

為此,您需要在代碼中添加<a>標記,代碼將如下所示。

替換echo? empty( $url_from_download ): $url_from_download; 'Download'; echo? empty( $url_from_download ): $url_from_download; 'Download'; 符合以下代碼

echo ! empty( $url_from_download ) ? '<a class="button" href=" ' . esc_url($url_from_download) . ' ">Download</a>' : '';

暫無
暫無

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

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