I’m trying to display the fields I have created in the recent order template of WooCommerce and I’m not very knowledgeable in PHP.
I have created a field called sessions and registered as a product post type. Once a user purchase a product I want thats customs fields “sessions” values to be displayed in the My account > Recent orders (template).
I tried looking for answers and solutions and I seem to be stuck.
Here is the customized code of my-order.php template that I have been working on. I have been hacking it for days and can’t seem to display this values in my recent orders table.
Updated – added images to and more description to clarify my problem
1.) As you can see here I created two types of fields and registered them as product post type
2.) Then I placed a value on those two fields I have created
3.) Once a user or customer purchase the item/product/package. I want those two values to be shown on the recent orders of the user my-account template under the column “Sessions”
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$my_orders_columns = apply_filters( 'woocommerce_my_account_my_orders_columns', array(
'order-number' => __( 'Package', 'woocommerce' ),
'sessions' => __( 'Session', 'woocommerce' ),
'order-total' => __( 'Package Prize', 'woocommerce' ),
'order-date' => __( 'Date', 'woocommerce' ),
'order-end-date' => __( 'End Date', 'woocommerce'),
'order-status' => __( 'Status', 'woocommerce' ),
'order-actions' => ' ',
) );
$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() )
) ) );
if ( $customer_orders ) : ?>
<h2><?php // echo apply_filters( 'woocommerce_my_account_my_orders_title', __( 'Recent Orders', 'woocommerce' ) ); ?></h2>
<table class="shop_table shop_table_responsive my_account_orders">
<thead>
<tr>
<?php foreach ( $my_orders_columns as $column_id => $column_name ) : ?>
<th class="<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ( $customer_orders as $customer_order ) :
$order = wc_get_order( $customer_order );
$item_count = $order->get_item_count();
?>
<tr class="order">
<?php foreach ( $my_orders_columns as $column_id => $column_name ) : ?>
<td class="<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
<?php elseif ( 'order-number' === $column_id ) : ?>
<?php foreach($order->get_items() as $item) {
$product_name = $item['name'];
} ?>
<?php echo $product_name;?>
<?php elseif ( 'session' === $column_id ) : ?>
<?php if (get_field('session_period', $product->id) ) : ?>
<?php endif; ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php echo sprintf( _n( '%s', '%s', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); ?>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>
<?php /* Order End Date */ ?>
<?php elseif ( 'order-end-date' === $column_id ) : ?>
<?php if (get_field('date_ended', $order->id) ) : ?>
<p class="sendungsnummer"><?php the_field('date_ended', $order->id); ?>
<?php endif; ?>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo wc_get_order_status_name( $order->get_status() ); ?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = array(
'pay' => array(
'url' => $order->get_checkout_payment_url(),
'name' => __( 'Pay', 'woocommerce' )
),
'view' => array(
'url' => $order->get_view_order_url(),
'name' => __( 'View', 'woocommerce' )
),
'cancel' => array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => __( 'Cancel', 'woocommerce' )
)
);
if ( ! $order->needs_payment() ) {
unset( $actions['pay'] );
}
if ( ! in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) {
unset( $actions['cancel'] );
}
/* -------- View Button --------
if ( $actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order ) ) {
foreach ( $actions as $key => $action ) {
echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>';
}
}
*/
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
Here we are going on a different approach than custom fields made with ACF plugin. We create a dedicated metabox with 2 fields inside it, located on the right side column in backend product pages, with this code:
Note: you will paste this code in the
function.php
file of active child theme or theme.HOW TO USE IT IN YOUR TEMPLATE CODE + THE CORRECTED MISTAKES:
You will not need to use ACF plugin for those simple fields. You will find also the missing code to retrieve order ID, Product ID and to display on My account > recent orders table, the sessions data:
That you need for:
… / …
… / …
You will access this data with product ID (or post ID) and the WordPress function
get_post_meta()
in your template:And you will use them, displaying their values this way:
… / …
This approach is more professional and cleaner:
This is based on a different problem but some kind of similar:
WooCommerce : Add custom Metabox to admin order page