在 opencart 3 中的订单历史页面中显示所有产品图像和名称
Show all products image and name in order history page in opencart 3
我想在订单历史记录中显示订单中的所有产品图片名称 details/get,为了实现同样的效果,我已将我的代码插入到结果循环中,就像下面的代码一样,但它只获取一个产品并且同一产品对每个订单可见。
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
}
}
- 订单-list.twig
此处的屏幕截图:
- 首页(订单历史)
截图:
问题出在你的循环中。您将产品附加到 $data['products'],而您应该将其附加到 $data['orders']['products']
- 在您的代码中重命名变量
$data['products']
到
$order_products
- 然后将其添加到订单数组
$data['orders'][] = array(
...
'order_products' => $order_products
...
);
- 并在 twig 文件中更改 for 循环
{% for order_product in order.order_products %}
<li>{{ order_product.name }}</li>
{% endfor %}
基本上,我们所做的是扩展订单对象以包含产品信息,然后将其显示在视图中。
我想在订单历史记录中显示订单中的所有产品图片名称 details/get,为了实现同样的效果,我已将我的代码插入到结果循环中,就像下面的代码一样,但它只获取一个产品并且同一产品对每个订单可见。
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
}
}
- 订单-list.twig 此处的屏幕截图:
- 首页(订单历史) 截图:
问题出在你的循环中。您将产品附加到 $data['products'],而您应该将其附加到 $data['orders']['products']
- 在您的代码中重命名变量
$data['products']
到
$order_products
- 然后将其添加到订单数组
$data['orders'][] = array(
...
'order_products' => $order_products
...
);
- 并在 twig 文件中更改 for 循环
{% for order_product in order.order_products %}
<li>{{ order_product.name }}</li>
{% endfor %}
基本上,我们所做的是扩展订单对象以包含产品信息,然后将其显示在视图中。