Opencart は Model-View-Controller フレームワーク (MVC) を使用します。このフレームワークでは、コントローラーがモデルと通信してデータを取得し、データを準備してからビュー (Opencart の .tpl ファイル) に渡し、適切に表示します。
あなたの場合、header.phpコントローラーでまだ準備されていないため、header.tplには$products配列のデータがありません。ヘッダー コントローラー (catalog/controller/header.php) の index() 関数で、モデルからすべてのデータを取得し、必要な方法で準備してからビューに渡します。
$this->load->model('catalog/category'); //
$this->load->model('catalog/product'); //Load our models so the controller can get data
$categories = $this->model_catalog_category->getCategories(0); //get all top level categories
$all_products = array();
foreach ($categories as $category) //go through each category and get all the products for each category
{
$category_products = $this->model_catalog_product->getProductsforCategoryId($category['category_id']); //returns product IDs for category
foreach ($category_products as $category_product)
{
$product_data = $this->model_catalog_product->getProduct($category_product); //fetch product data for this product then add it to our array of all products
$all_products[] = array(
'href' => $this->url->link('product/product', 'product_id=' . $product_data['product_id']),
'name' => $product_data['name']
);
}
}
$this->data['products'] = $all_products; //Now pass our product array data to the view, in the view this will be the $products array
これは、すべての商品が最上位のカテゴリのみに属し、サブカテゴリには属していないことを前提としています。将来、最上位カテゴリのサブカテゴリを作成する場合は、それらのサブカテゴリをループして、それぞれの製品を取得する必要があります。