I am creating navigation for my store. I am new to magento. I am having problem to load product under that category. It is possible for me to print all product under all categories but I don't know how to load products under its specific category.
This is my code
<?php $_menu = $this->renderCategoriesMenuHtml($this->getSelectedCategoryId(), 0, 'level-top') ?>
<?php
if ($this->getTitle() != '') {
$title = $this->getTitle();
} else {
$title = 'Categories';
}
?>
<?php
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
?>
<?php if ($_menu): ?>
<div class="block vertical-menu">
<div class="title-block" style="margin-bottom:0px;"><h4><?php echo $this->__($title) ?></h4></div>
<div class="block-content">
<?php $children = Mage::getModel('catalog/category')->getCategories(2);?>
<ul class="content-navigation">
<?php foreach($children as $category){ ?>
<li>
<?php echo $category->getName();?>
<? foreach($category->getId()){ ?> // here I want to load product as per their category
<ul>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
</div>
<?php else: ?>
<?php if (Mage::helper('mpanel')->acceptToUsePanel()): ?>
<div class="block vertical-menu">
<div class="block-title"><strong><span><?php echo $this->__($title) ?></span></strong></div>
<div class="block-content">
<p><?php echo $this->__('Have no categories.'); ?></p>
</div>
</div>
<?php endif; ?>
<?php endif; ?>
try to use following code
$category = Mage::getModel('catalog/category')->load(' Mage::registry('current_category')->getId() '); //You will get current category id use passing value.
$productCollection = $category->getProductCollection();
foreach($productCollection as $product)
{
echo $product->getName();
}
It work..
Related
on each products page I currently have displayed the products subcategory, but only one level deep. So if in categories I have Brother > MFC > B4564 > TN450.... and I am on the TN450's product page, down below it will only show BN4564 . I need it also show The mfc and the brother part. I am not sure how to do this.. any ideas? here is my current code.
.TPL file
<!-- Display product categories -->
<?php foreach ($catprod as $catp)
{
?><?php echo $catp['name']; ?> <?php
}`
controller .php file
$data['catprod'] = array();
$product_category = $this->model_catalog_product->getCategories($this->request->get['product_id']);
foreach ($product_category as $prodcat)
{
$category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
if ($category_info)
{
$data['catprod'][] = array('name' => $category_info['description'],'href' => $this->url->link('product/category', 'path=' . $category_info['category_id']));
}
}`
which version of opencart?
this should work for version 1:
open catalog/view/theme/default/template/module/category.tpl
remove its contents and insert:
<div class="box" id="categories-menu">
<div class="box-heading"><?php echo $heading_title; ?></div>
<div class="box-content">
<ul class="box-category" id="box-category">
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<a href="<?php echo $category['href']; ?>" class="active list-group-item" ><?php echo $category['name']; ?></a>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php if ($category['children']) { ?>
<ul class="submenu">
<?php foreach ($category['children'] as $child) { ?>
<li>
<?php if ($child['category_id'] == $child_id) { ?>
- <?php echo $child['name']; ?>
<?php } else { ?>
- <?php echo $child['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
I want to Show Multilevel Dropdown menu in opencart 2.0 category module
Menu>submenu>submenu1>submenulevel2>level3>
Can you please explain if you want this in the Admin area or the customer front end?
In which case in category.tpl just modify or write something like this:
<?php if ($categories) { ?>
<?php if (count($categories) <= 5) { ?>
<div class="col-sm-3 nosidepadding">
<ul>
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?></li>
<?php } ?>
</ul>
</div>
<?php } else { ?>
<?php foreach (array_chunk($categories, ceil(count($categories) / 4)) as $categories) { ?>
<div class="col-sm-3 nosidepadding">
<ul>
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?></li>
<?php } ?>
</ul>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
I am trying to figure out how can I show subcategories for selected parent category in search.php. I have code which shows subcategories of all categories, but how can I modify this code to show subcategories for only selected category?
this is my code:
<?php while(osc_has_categories()) { ?>
<?php if(osc_count_subcategories() > 0) { ?>
<ul>
<?php while(osc_has_subcategories()) { ?>
<li>
<strong><?php echo osc_category_name(); ?></strong></label>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
thank you
When in a loop while(osc_has_categories()), it allows you to use osc_category_* helpers.
You could do something like:
<?php while(osc_has_categories()) { ?>
<?php if(osc_category_id() === 98): ?>
<?php if(osc_count_subcategories() > 0) { ?>
<ul>
<?php while(osc_has_subcategories()) { ?>
<li>
<strong><?php echo osc_category_name(); ?></strong>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php endif; ?>
<?php } ?>
On my Magento homepage I try to list the subcategories from a certain category with some values from custom category attributes in it. The list is working but I can't obtain the values from the custom category attributes.
The output is blank. What I am doing wrong? The relevant attributes are category_subtitle and category_slidertext.
<?php
$_helper = Mage::helper('catalog/category');
$productsChildren = Mage::getModel('catalog/category')->getCategories(3);
foreach ($productsChildren as $productCat) {
?>
<li>
<div class="content-wrapper">
<div class="content">
<h2><?php $_category_subtitle = $productCat->getData('category_subtitle'); if($_category_subtitle): ?><span><?php echo $_category_subtitle; ?></span><?php endif; ?></h2>
<?php $_category_slidertext = $productCat->getData('category_slidertext'); if($_category_slidertext): ?><h3><?php echo $_category_slidertext; ?></h3><?php endif; ?>
<?php echo $this->__('View our products') ?> >
</div>
</div>
</li>
<?php } ?>
Try:
$productsChildren = Mage::getModel('catalog/category')->getCategories(3,0,false,true,false);
$productsChildren->addAttributeToSelect('category_subtitle')
->addAttributeToSelect('category_slidertext');
That will work if $productsChildren is an instance of the category collection.
I wrote the code below to put together a customized menu of categories. Everything works fine, but would like the order of the categories were the same order as defined in the administrator panel where there drap and drop functionality.
<?php
$subCats = Mage::getModel('catalog/category')->load(76)->getChildren();
$dispositosCatIds = explode(',',$subCats);
?>
<ul class="menu">
<?php $controleNum = 0; ?>
<?php foreach($dispositosCatIds as $dispositoCatId): ?>
<?php $aparelhoCat = Mage::getModel('catalog/category')->load($dispositoCatId); ?>
<?php if($aparelhoCat->getIsActive()): ?>
<li class="<?php print $controleNum ? '' : 'submenu first'; ?>"><a class="drop" href="<?php echo $aparelhoCat->getUrl(); ?>"> <span><?php echo $aparelhoCat->getName(); ?></span></a> <!--Begin 6 column Item -->
<div class="dropdown_6columns">
<div class="inner"><span class="title"><?php echo $aparelhoCat->getName(); ?></span>
<div class="col_2">
<div class="col_2 firstcolumn"><img src="<?php echo $aparelhoCat->getImageUrl(); ?>" alt="<?php echo $aparelhoCat->getName(); ?>" /></div>
</div>
<div class="col_4" style="margin-bottom: 20px;">
<?php echo $aparelhoCat->getDescription(); ?>
</div>
<div class="col_2 categorias-super"><span class="title_col">Produtos para <?php echo $aparelhoCat->getName(); ?></span>
<?php $subSubCats = $aparelhoCat->getChildrenCategories();?>
<?php if (count($subSubCats) > 0): ?>
<?php //$controleNumLI = 0; ?>
<ul style="list-style: none; float: none !important;">
<?php foreach($subSubCats as $_subcategory): //Rodando entre as categorias de Um dispositivo ?>
<?php if($_subcategory->getIsActive()): ?>
<li class="level1 <?php //print $controleNumLI ? '' : 'first'; ?>"> <?php echo $_subcategory->getName(); ?></li>
<?php //$controleNumLI += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
</li>
<?php $controleNum += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I tried to use other modes (based here) can do this, but I could not. The problem that the function returns getChildren() is a string with IDs in ascending order.
Some Ideas?
This is the code I use to display category in a dropdown box in the order of the admin... the key is setOrder('path','ASC')
$categories = array();
$_categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('path','ASC')
->addAttributeToSelect('name')->load();
foreach($_categories as $cat){
$level = $cat->getLevel() - 1;
$pad = str_repeat("----", ($level > 0) ? $level : 0);
$categories[] = array('value' => $cat->getEntityId(), 'label' => $pad . ' ' . $cat->getName());
}
print_r($categories);
You could do something like this: create array tree from array list
I got it:
$dispositovosCategoryId = 76;
$dispositovosCategoryIds = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$dispositovosCategoryId))
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('position','ASC')
->addAttributeToSelect('name','url','image')
->load();