Home › Forums › Front-end Issues › Display custom field on woocommerce checkout page
I’m having some trouble trying to display the field I have created in the checkout page of woocommerce and i’m not very knowledgeable in PHP. I have created a field called sessions registered it as a product post type. Once a user purchase a product I want the custom field(sessions) to also be displayed.
Here is the template of the view-order of woocommerce.
<?php
/**
* Order Item Details
*
* This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-item.php.
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
return;
}
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="product-name">
<?php
$is_visible = $product && $product->is_visible();
echo apply_filters( 'woocommerce_order_item_name', $is_visible ? sprintf( '<a href="%s">%s</a>', get_permalink( $item['product_id'] ), $item['name'] ) : $item['name'], $item, $is_visible );
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong>', $item );
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
$order->display_item_meta( $item );
$order->display_item_downloads( $item );
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
?>
</td>
<td class="product-total">
<?php echo $order->get_formatted_line_subtotal( $item ); ?>
</td>
</tr>
<?php if ( $show_purchase_note && $purchase_note ) : ?>
<tr class="product-purchase-note">
<td colspan="3"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
</tr>
<?php endif; ?>
I have no idea or understanding where I would insert the_field(sessions)
to display it on the checkout
Hi @clestcruz
You need to provide the ID of the product as the second parameter for the the_field() function because you called it outside The Loop. I believe you can do it like this:
`the_field(‘sessions’, $product->ID);
$order->display_item_meta( $item );’
I hope this helps π
I am trying to add a new field in the checkout page but I am having an error. I am having this line as error
if (!$_POST[‘customised_field_name’]) wc_add_notice(__(‘Please enter value.’) , ‘error’);
I have completely checked the code but still, I am having an error. I have followed the WooCommerce checkout page tutorial and implement the same as discussed in the article. Can you help me to resolve this issue?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
βEver wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
π https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.