Support

Account

Home Forums Front-end Issues Display custom field on woocommerce checkout page

Solving

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( '&times; %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?

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Display custom field on woocommerce checkout page’ is closed to new replies.