简体   繁体   中英

How to create a product only menu in Opencart?

I'm building a site that is starting only with only 2 products, so a category menu is not appropriate and I'm attempting to create a product only menu.

I'm using a commercial theme and it automagically generates a category menu in header.tpl

I need to create a product menu but not being a php coder it gets tricky, so far I have:

<ul id="topnav">
<?php foreach ($products as $product) { ?>
<li><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></li>
<?php } ?>
</ul>

Does anyone know where I'm going wrong here?

Opencart uses the Model-View-Controller framework (MVC) where the Controller talks to the Model to retrieve data, prepares the data and then passes it to the View (the .tpl files in Opencart) which then displays it appropriately.

In your case the header.tpl doesn't have the data for $products array because it has not been prepared in the header.php controller yet. So in the index() function of the header controller (catalog/controller/header.php) let's get all the data from our models, prepare it how we want and then pass it to the view:

$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

This is assuming all your products belong to only top level categories and not any subcategories. If you do create subcategories of the top level categories in the future you will need to loop through those subcategories and get the products for each one.

I've found the answer thanks to a kind member of the OpenCart forum.

Just put this code where you need it:

<?php
    $this->load->model('catalog/product');

    $products_1 = $this->model_catalog_product->getProducts($data = array());                                   
    if ($products_1) {$output = '<ul id="topnav">';}                                               
    foreach ($products_1 as $product_1) {                                                         
       $output .= '<li>';                                                                                                         
       $unrewritten  = $this->url->link('product/product', 'product_id=' . $product_1['product_id']);                        
       $output .= '<a href="'.($unrewritten).'">' . $product_1['model'] . '</a>';                        
    }
    if ($products_1) {$output .= '</ul>';}                                             
    echo $output;                                                          
    ?>

Credit goes to the forum member here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM