Skip to content
Advertisement

How to add order items into a table using php and html

I want to create an order table but a little confused on how to add each it in its own row and column in the table.

function order_table_summary(){
        
    // Get and Loop Over Order Items
    foreach ( $order->get_items() as $item_id => $item ) {
       $name = $item->get_name();
       $quantity = $item->get_quantity();
       $item->get_total();
    }
    
    return '<table>
            <tr>
            <th>Product</th>
            <th>Quantity</th>
            <th>Price</th>
            </tr>
            <tr>
            // goes here
            </tr>
            <tr>
            <th colspan="2"></th>
            <td>' . $order->get_line_total() . '</td>
            </tr>
        </table>
        ';
            
    }

Advertisement

Answer

DO something like that. See the below code

function order_table_summary(){
 $html = '';   
// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {

    $html .= '<table>
        <tr>
        <th>Product</th>
        <th>Quantity</th>
        <th>Price</th>
        </tr>
        <tr>
            <td>'.$item->get_name().'</td>
            <td>'.$item->get_quantity().'</td>
            <td>'.$item->get_total().'</td>
        </tr>
        <tr>
        <th colspan="2"></th>
        <td>' . $order->get_line_total() . '</td>
        </tr>
    </table>';

}

    return $html;
        
}

cheers,

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement