douyi1855 2014-06-05 12:35
浏览 105

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?

  • 写回答

1条回答 默认 最新

  • dqunzip3183 2014-06-05 14:50
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题