wordpresswoocommercewordpress-theminghook-woocommercewoocommerce-theming

Show the value of the custom field of the order on the My Account Front-end page


I would like to show the value of the custom fields filled during the order also in the frontend my account page. This is the URL of the location:

https://example.it/my-account/view-order/860/

I would like the customized fields to be seen in the order summary page of the front end, also because the user does not have access to the bulletin board and if he wanted to check the fields filled in during the order he could not do it without it. The site is made with Wordpress, I use Divi as a theme and for orders I use the woocomerce plug-in


Solution

  • I solved with this

    // aggiungere il valore dei campi custom nella pagina il mio account/ order
    add_action( 'woocommerce_order_details_after_order_table', 'display_custom_fields_in_account_order_pages' );
    function display_custom_fields_in_account_order_pages( $order ){ 
        $quantity = $order->get_meta('cstm_items_count'); // Get items quantity count from meta data  
        
        echo '<div class="order_data_column" style="width: 100% !important;">
            <h2>' . __( 'Iscritti' ) . '</h2>
            <table class="iscritti">
            <thead>
            <tr>
            <th></th>
            <th>Nome e cognome</th>
            <th>Email</th>
            <th>Numero</th>
            <th>Indirizzo</th></tr></thead>';
            $i = 0;
            for( $k=1; $k <= $quantity; $k++ ) {
                $i++;
                echo '<tr><td><strong>'. __("Dati corsista n. ") . $i . ':</strong></td>
                <td>'. $order->get_meta('cstm_full_name'.$i) . '</td>
                <td>'. $order->get_meta('cstm_email'.$i) . '</td>
                <td>'. $order->get_meta('cstm_phone'.$i) . '</td>
                <td>'.$order->get_meta('cstm_address'.$i) . '</td></tr>';
            }
            echo '</table>
        </div>';
    }