Opencart:在订单发票中显示产品属性

what I want to do is display the product Attributes on the invoice within OpenCart. I'm a PHP beginner, I can do few things. I know the basics of the MVC and OpenCart.

I need to pull the $product['attributes] for each product in an order and load it into the Controller file for me to use within the order_invoice.tpl View file.

What I'm thinking of so far is:

 'attributes' => $this->model_catalog_product->getProductAttributes($result['product_id']);

adding this to the Controller file for the Order invoice, but I'm unsure where and how, also, if a product has more than one attribute, I'll need to load this into an array.

Then on the order_invoice.tpl I'll need to echo something along the lines of this

 <?php foreach ($order['product'] as $product) { ?>
     <?php foreach ($product['attributes'] as $attribute) { ?>

        <?php echo $product['attribute']; ?>

     <?php } ?> // END FOREACH ATTRIBUTE
<?php } ?> // END FOREACH PRODUCT

Now I know this isn't "aesthetically" the correct way to echo PHP, it should all be between one set of PHP tags, but for a beginner like me its easier to see what I'mm doing and if I do syntax errors.

Can someone help me in how to write this so it works fully?

You are doing and thinking very well.

The line for loading of attributes could be placed somewhere here (in controller, method invoice()):

foreach ($products as $product) {
    $attributes = $this->model_catalog_product->getProductAttributes($product['product_id']);

This already loads an array of all attributes linked with the product. Now assign those attributes to the product few lines below:

                $product_data[] = array(
                    'name'     => $product['name'],
                    'model'    => $product['model'],
                    'option'   => $option_data,
                    'quantity' => $product['quantity'],
                    'price'    => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
                    'total'    => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value'])
                    'attributes' => $attributes
                );

That should be all for controller, now just use the code for template You are asking about in Your question. Using <?php and ?> on each line is not unaesthetic rather recommended way of outputting of PHP. When dealing with templates consider the code as HTML code and that HTML has to have priority. Then all PHP modifications to the HTML code should be placed on it's separate line to preserve better readiness. The exception might be echoing of variables, e.g.

<div class="some-class"><?php echo $myVariable ?></div>

but

<ul>
    <?php foreach ($products as $product) { ?>
    <li><?php echo $product['name'] ?></li>
    <?php } ?>
</ul>

The only exception has to be done when using the switch statement, as the first case has to be within the same <?php ... ?> block, e.g.

<div>
<?php switch ($var) {
    case true: ?>
    <?php echo 'THIS IS GONNA HAPPEN.' ?>
    <?php break ?>
<?php } ?>

But I personally recommend to avoid using switch statements in templates at all costs because You can see even from the above example that it creates quite big mess of HTML and PHP tags with worse readability... Use if-elseif-else statement instead.