[英]How to Add Custom Data as a New Column in the WooCommerce My Orders list in My Account page?
I am trying to add 1 column to WooCommerce > My Account > Order Page.我正在尝试将 1 列添加到 WooCommerce > 我的帐户 > 订单页面。
With Jet-Engine I added a field called "url-from-download" inside the Customer Order in WooCommerce.使用 Jet-Engine,我在 WooCommerce 的客户订单中添加了一个名为“url-from-download”的字段。
There inside the customer order I select the PDF.里面有客户订单我select PDF。
The code below adds the Invoice column , and a Text called "Downloads" , but it cannot extract the File as a url for me to download.下面的代码添加了发票列和一个名为“下载”的文本,但它无法将文件提取为 url 供我下载。
Where I'm I getting it wrong??我哪里弄错了??
/**
* 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' );
I added a field called "url-from-download"我添加了一个名为“url-from-download”的字段
you have stated in question that your field is called url-from-download
but in your code, you're using url_from_download
您有疑问地表示您的字段名为url-from-download
但在您的代码中,您使用的是url_from_download
There is a difference in dash (-) & underscore (_) so make sure you're using the correct meta key in get_post_meta
破折号 (-) 和下划线 (_) 有所不同,因此请确保您在get_post_meta
中使用了正确的元键
UPDATE更新
as per the discussion, I am assuming you're getting the url but you need it as button style that you can click.根据讨论,我假设你得到 url 但你需要它作为你可以点击的按钮样式。
To do that you need to add <a>
tag in your code, the code will be like this.为此,您需要在代码中添加<a>
标记,代码将如下所示。
Replace echo? empty( $url_from_download ): $url_from_download; 'Download';
替换echo? empty( $url_from_download ): $url_from_download; 'Download';
echo? empty( $url_from_download ): $url_from_download; 'Download';
line with below code符合以下代码
echo ! empty( $url_from_download ) ? '<a class="button" href=" ' . esc_url($url_from_download) . ' ">Download</a>' : '';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.