Reputation: 1655
I have a dropdown list containing categories and subcategories from my OpenCart shop, however I cannot seem to get the SEO keyword URL to show as the option value:
<select name="cat_id" id="category-select" class="span4">
<option value="">Browse by category</option>
<?php
$cats = $this->model_catalog_category->getCategories();
foreach ($cats as $cat) {
echo '<option value="' . $this->url->addRewrite('product/category', '&category_id=' . $cat['category_id']) . '">' . $cat['name'] . '</option>';
$subcats = $this->model_catalog_category->getCategories($cat['category_id']);
foreach ($subcats as $subcat) {
echo '<option value="' . $this->url->addRewrite('product/category', '&category_id=' . $subcat['category_id']) . '">- ' . $subcat['name'] . '</option>';
}
}
?>
</select>
I am new to OpenCart and not quite sure about what needs to go in the addRewrite function. Can't seem to find any mention of this in the OpenCart documentation either.
Upvotes: 0
Views: 3442
Reputation: 16065
That's probably because of wrong method used. For SEO URL's You'd have to use the link()
method. And of course You're forgetting about the MVC. The right approach would be to modify the controller which would load and prepare the data that will be passed to template then. Template is supposed to draw/present the data only...
So let's assume it is the category
controller and template, so let's edit the right controller first (catalog/controller/product/category.php
):
$this->data['my_categories'] = array();
foreach($this->model_catalog_category->getCategories() as $category) {
$this->data['my_categories'][] = array(
'title' => $category['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id']),
);
}
This is only for example thus I do not load subcategories here, but the approach would be the same.
Now present the data in the template (so catalog/view/theme/<YOUR_THEME>/template/product/category.tpl
):
<select name="cat_id" id="category-select" class="span4">
<option value="">Browse by category</option>
<?php if($my_categories) { ?>
<?php foreach ($my_categories as $category) { ?>
<option value="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
EDIT: The SEO link for subcategory should be:
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $subcategory['category_id'])
Upvotes: 1