在 Opencart 3 上,购物车页面显示用户已放置在那里的产品。任务是按制造商对这些商品进行分类,并分别显示制造商的名称。得到子组。它应该看起来像这样。
Название производителя 1
Товар 1
Товар 2
Товар 3
Название производителя 2
Товар 5
Товар 6
Товар 7
目前,使用 usort,可以按制造商对所有产品进行排序
usort($data['products'], function($a, $b) {
return $a['manufacturer'] <=> $b['manufacturer'];
});
结果,我得到了所需的排序。该数组按您需要的顺序排列。但是现在如何将商店的名称 ['manufacturer'] 分隔成一个单独的名称,使其看起来像上面的示例?
Array
(
[0] => Array
(
[cart_id] => 23
[thumb] => http://magis.loc/image/cache/catalog/Products/shoe3-90x90.jpg
[name] => Кроссовки мужские белые для повседневной носки
[model] =>
[upc] => 8
[manufacturer] => Bykilian
[manufacturers] => http://magis.loc/index.php?route=product/manufacturer/info&manufacturer_id=12
[option] => Array
(
)
[recurring] =>
[quantity] => 2
[stock] => 1
[reward] =>
[price] => 179.00 $
[total] => 358.00 $
[href] => http://magis.loc/index.php?route=product/product&product_id=52
)
[1] => Array
(
[cart_id] => 26
[thumb] => http://magis.loc/image/cache/catalog/Products/shoe4-90x90.jpg
[name] => Бутсы спортивные мужские
[model] => 36 - 1 шт
[upc] => 8
[manufacturer] => Bykilian
[manufacturers] => http://magis.loc/index.php?route=product/manufacturer/info&manufacturer_id=12
[option] => Array
(
)
[recurring] =>
[quantity] => 2
[stock] => 1
[reward] =>
[price] => 80.00 $
[total] => 160.00 $
[href] => http://magis.loc/index.php?route=product/product&product_id=53
)
[2] => Array
(
[cart_id] => 29
[thumb] => http://magis.loc/image/cache/catalog/Products/shoe5-90x90.jpg
[name] => Кроссовки мужские для повседневной носки
[model] =>
[upc] => 8
[manufacturer] => Kidsnail
[manufacturers] => http://magis.loc/index.php?route=product/manufacturer/info&manufacturer_id=13
[option] => Array
(
)
[recurring] =>
[quantity] => 2
[stock] => 1
[reward] =>
[price] => 194.00 $
[total] => 388.00 $
[href] => http://magis.loc/index.php?route=product/product&product_id=54
)
[3] => Array
(
[cart_id] => 24
[thumb] => http://magis.loc/image/cache/catalog/Products/shoe7-90x90.jpg
[name] => Туфли женские открытые
[model] =>
[upc] => 8
[manufacturer] => Longstormaier
[manufacturers] => http://magis.loc/index.php?route=product/manufacturer/info&manufacturer_id=11
[option] => Array
(
)
[recurring] =>
[quantity] => 4
[stock] => 1
[reward] =>
[price] => 199.00 $
[total] => 796.00 $
[href] => http://magis.loc/index.php?route=product/product&product_id=56
)
[4] => Array
(
[cart_id] => 25
[thumb] =>
[name] => Туфли женские на каблуке
[model] =>
[upc] => 6
[manufacturer] => Longstormaier
[manufacturers] => http://magis.loc/index.php?route=product/manufacturer/info&manufacturer_id=11
[option] => Array
(
)
[recurring] =>
[quantity] => 3
[stock] => 1
[reward] =>
[price] => 150.00 $
[total] => 450.00 $
[href] => http://magis.loc/index.php?route=product/product&product_id=51
)
)
这是默认函数的样子,它输出一个数组
foreach ($products as $product) {
$product_info = $this->model_catalog_product->getProduct($product['product_id']);
$product_total = 0;
$data['products'][] = array(
'cart_id' => $product['cart_id'],
'thumb' => $image,
'name' => $product['name'],
'model' => $product['model'],
'upc' => $product_info['upc'],
'manufacturer' => $product_info['manufacturer'],
'manufacturers' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $product_info['manufacturer_id']),
'option' => $option_data,
'recurring' => $recurring,
'quantity' => $product['quantity'],
'stock' => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')),
'reward' => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''),
'price' => $price,
'total' => $total,
'href' => $this->url->link('product/product', 'product_id=' . $product['product_id'])
);
}
重建阵列
该脚本将输出:
甚至不需要对 $products 数组进行排序。