繁体   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