John Davidson

php - How to Add Custom Data as a New Column in the WooCommerce My Orders list in My Account page?

0 comments
Message:


I am trying to add 1 column to WooCommerce > My Account > Order Page.


With Jet-Engine I added a field called "url-from-download" inside the Customer Order in WooCommerce.


There inside the customer order I select the 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.


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' );


My Answer:
It looks like you have a small typo in your code. In the `get_post_meta` function, you are using `'url_from_download'` as the meta key, but in your description, you mentioned that the field is called `'url-from-download'`.

You need to make sure that the meta key matches exactly what you have set in your Jet-Engine field.

Try updating the `get_post_meta` function to use the correct meta key:

php
$url_from_download = get_post_meta( $order->get_id(), 'url-from-download', true ); // Get custom order meta


After making this change, the code should be able to retrieve the URL from the custom field and display it in the new column on the My Account > Orders page.

Rate this post

5 of 5 based on 3185 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us