在opencart 3的订单历史记录页面中显示所有产品图片和名称

时间:2019-02-19 10:53:48

标签: php opencart opencart-3

我想在订单历史记录中显示商品详细信息/在订单中获取所有商品图片名称,为实现相同的目的,我已将我的代码插入到结果循环中,就像下面的代码一样,但它仅获取一个商品并且该商品相同每个订单都可以看到该产品。

1.catalog / controller / account / order.php

public function index() {
...
foreach ($results as $result) {
...

//for product for loop

            $order_info = $this->model_account_order->getOrder($result['order_id']);

            if ($order_info) {
            $this->load->model('catalog/product');
            $this->load->model('tool/upload');



            // Products


            $data['products'] = array();

            $products = $this->model_account_order->getOrderProducts($result['order_id']);

            foreach ($products as $product) {
                $option_data = array();

                $options = $this->model_account_order->getOrderOptions($result['order_id'], $product['order_product_id']);

                foreach ($options as $option) {
                    if ($option['type'] != 'file') {
                        $value = $option['value'];
                    } else {
                        $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

                        if ($upload_info) {
                            $value = $upload_info['name'];
                        } else {
                            $value = '';
                        }
                    }

                    $option_data[] = array(
                        'name'  => $option['name'],
                        'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
                    );
                }

                $product_info = $this->model_catalog_product->getProduct($product['product_id']);

                if ($product_info) {
                    $reorder = $this->url->link('account/order/reorder', 'order_id=' . $result['order_id'] . '&order_product_id=' . $product['order_product_id'], true);
                } else {
                    $reorder = '';
                }

                $data['products'][] = 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']),
                    'reorder'  => $reorder,
                    'return'   => $this->url->link('account/return/add', 'order_id=' . $result['order_id'] . '&product_id=' . $product['product_id'], true)
                );

                // Totals
                $data['totals'] = array();

                $totals = $this->model_account_order->getOrderTotals($result['order_id']);

                    foreach ($totals as $total) {
                    $data['totals'][] = array(
                        'title' => $total['title'],
                        'text'  => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
                    );
                }




            }

            //end for loop of product
            }
}
  1. order-list.twig 此处的屏幕截图:

enter image description here

  1. 首页(订购记录) 屏幕快照:

enter image description here

1 个答案:

答案 0 :(得分:0)

问题出在您的循环中。当您将产品附加到$ data ['orders'] ['products']

时,就是将产品附加到$ data ['products']

  1. 在代码中重命名变量
    $data['products']

    $order_products
  1. 然后将其添加到订单数组
    $data['orders'][] = array(
        ...
        'order_products' => $order_products
        ...
    );
  1. 然后在树枝文件中更改for循环
    {% for order_product in order.order_products %}
    <li>{{ order_product.name }}</li>
    {% endfor %}

基本上,我们正在做的是扩展orders对象以包括产品信息,然后将其显示在视图中。