I have recently started working in magento, and my boss has given me the task of displaying categories in the left column. I have the static html file which I pasted inside the .phtml file and called it from the xml block and it displays just the static stuff in the left column.
I want them to be dynamic so that the categories can be added or removed from the backend.
The html that I have is in table format and I want to insert the php code into that same html so that the design is not disturbed and it becomes dynamic.
This might be what you're looking for:
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
<div class="block block-layered-nav">
<div class="block-title">
<strong><span><?php echo $this->__('Browse By') ?></span></strong>
</div>
<div class="block-content">
<dl id="narrow-by-list2">
<dt><?php echo $this->__('Category') ?></dt>
<dd>
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)
</li>
<?php endif; ?>
<?php endforeach ?>
</ol>
</dd>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list2')</script>
</div>
</div>
<?php endif; ?>
I used it here: http://marblesthebrainstore.com
Related
I have been searching high and low for this answer. I would be extremely grateful if someone can point me in the right direction. I would like to show all other child categories on a child category in my sidebar navigation.
As an example...
INSTEON
INSTEON Starter Kits
INSTEON Responders
INSTEON Controllers
INSTEON Accessories
When selecting a child category of INSTEON (INSTEON Responders as an example) - I would still like the other children to display (please see http://www.smarthome.com.au/insteon.html). Ideally the current child should be bold and the other children normal.
I believe I need to edit app/design/frontend/theme/subtheme/template/catalog/navigation/left.phtml
<!--Added by Brad - Get current category -->
<?php $currentCategory = Mage::registry("current_category"); ?>
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
<div class="block block-layered-nav">
<div class="block-title">
<strong><span><?php echo $this->__('Browse By') ?></span></strong>
</div>
<div class="block-content">
<dl id="narrow-by-list2">
<!--Added by Brad - Display current category name -->
<dt><h2><?php echo $currentCategory->getName(); ?><?php echo $this->__('') ?></h2></dt>
<dd>
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->escapeHtml($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)
</li>
<?php endif; ?>
<?php endforeach ?>
</ol>
</dd>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list2')</script>
</div>
</div>
<?php endif; ?>
Any help is greatly appreciated.
<?php $currentCategory = Mage::registry("current_category"); ?>
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $parent = $this->getCurrentCategory()->getParentCategory()->getId(); ?>
<?php $parent = Mage::getModel('catalog/category')->load($parent); ?>
<?php $siblings = explode(',', $parent->getChildren()); ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
<div class="block block-layered-nav block-layered-nav--no-filters">
<div class="block-title">
<strong><span><?php echo $this->__('Browse By') ?></span></strong>
</div>
<div class="block-content toggle-content open">
<p class="block-subtitle block-subtitle--filter"><?php echo $this->__('Filter') ?></p>
<dl id="narrow-by-list2">
dt><?php echo $this->__('Category') ?></dt>
<dd>
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>
<?php echo $this->escapeHtml($_category->getName()) ?>
<span class="count">(<?php echo $_category->getProductCount() ?>)</span>
</a>
</li>
<?php endif; ?>
<?php endforeach ?>
</ol>
</dd>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list2')</script>
</div>
</div>
<?php elseif(count($siblings > 1)): ?>
<div class="block block-layered-nav block-layered-nav--no-filters">
<div class="block-title">
<strong><span><?php echo $this->__('Browse By') ?></span></strong>
</div>
<div class="block-content toggle-content open">
<p class="block-subtitle block-subtitle--filter"><?php echo $this->__('Filter') ?></p>
<dl id="narrow-by-list2">
dt><?php echo $this->__('Category') ?></dt>
<dd>
<ol>
<?php foreach($siblings as $sibling_id): ?>
<?php $sibling = Mage::getModel('catalog/category')->load($sibling_id); ?>
<li>
<a href="<?php echo $this->getCategoryUrl($sibling) ?>"<?php if ($this->isCategoryActive($sibling)): ?> class="current"<?php endif; ?>>
<?php echo $this->escapeHtml($sibling->getName()) ?>
<span class="count">(<?php echo $sibling->getProductCount() ?>)</span>
</a>
</li>
<?php endforeach ?>
</ol>
</dd>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list2')</script>
</div>
</div>
Just use
$parent_category_id = $this->getCurrentCategory()->getParentCategory()->getId();
to get the parent category id. Then
Mage::getModel('catalog/category')->load($parent_category_id);
to load the entire parent category. With that you can load the parent category's children (see Get Child categories magento).
--EDIT--
<!--Added by Brad - Get current category -->
<?php $currentCategory = Mage::registry("current_category"); ?>
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<!-- My code -->
<?php $parent = $this->getCurrentCategory()->getParentCategory()->getId(); ?>
<?php $parent = Mage::getModel('catalog/category')->load($parent); ?>
<?php $siblings = explode(',', $parent->getChildren()); ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
<div class="block block-layered-nav">
<div class="block-title">
<strong><span><?php echo $this->__('Browse By') ?></span></strong>
</div>
<div class="block-content">
<dl id="narrow-by-list2">
<!--Added by Brad - Display current category name -->
<dt><h2><?php echo $currentCategory->getName(); ?><?php echo $this->__('') ?></h2></dt>
<dd>
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->escapeHtml($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)
</li>
<?php endif; ?>
<?php endforeach ?>
</ol>
</dd>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list2')</script>
</div>
</div>
<!-- this will only show if there are no child categories of current child (and there are siblings) -->
<?php elseif(count($siblings > 1)): ?>
<!-- show sibling categories if have any -->
<?php foreach($siblings as $sibling_id): ?>
<?php if($sibling_id == $currentCategory->getId()): ?>
<!-- CURRENT CATEGORY -->
<?php else: ?>
<!-- SIBLING CATEGORY -->
<?php $sibling = Mage::getModel('catalog/category')->load($sibling_id); ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
Hi All I want to show 4 popular products on the home page of my magento 1.7.1 install. (The I can select by putting them into a category).
I've set this up by creating a hidden category called popularhome and added 4 products into it.
I've included this in a static block into my home page template by using:
{{block type="catalog/product_list" column_count="4" category_id="17" template="catalog/product/listhome.phtml"}}
My listhome.phtml template looks like this:
<div class="row popularproducts">
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<div class="alert fade in">
<a class="close" data-dismiss="alert">×</a>
<?php echo $this->__('There are no products matching the selection.') ?>
</div>
<?php else: ?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<?php else: ?>
<?php // Grid Mode ?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<?php endif ?>
<div class="c3">
<?php if(($i-1)%$_columnCount==0): ?><?php elseif($i%$_columnCount==0): ?><?php endif; ?>
<div class="thumbnail">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(225); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<div class="caption">
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<?php echo $this->getPriceHtml($_product, true) ?>
</div>
</div>
</div>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
<?php endif ?>
<?php endforeach ?>
<?php endif; ?>
<?php endif; ?>
</div>
However this is only showing 2 of the 4 products yet I cannot figure out why? Does anyone see anything I am missing?
Thanks!
First you need to check ,the products that you want to be appear in category page,
Is they really belong to that categories.
After that make sure Backend > Manage Categories > Edit categories > Display settings >
is Anchor = yes Then re-index your catalog.
To verify you need to cross check catalog_category_product and catalog_category_product_index.
If the category id and product id is mapped properly in these tables you should be good to go.
hope this will sure help you!
I tried this link and displaying all the categories in home page but it is displaying only the top level categories. I want to display all the categories.
<div class=”block block-verticalmenu”>
<div class=”block-title”>
<strong><span><?php echo $this->__(‘Categories’) ?></span></strong>
</div><!–End block block-cart–>
<div class=”block-content”>
<ul id=”ma-accordion” class=”accordion”>
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php echo $this->drawItem($_category) ?>
<?php endforeach ?> </ul>
</div><!–End Of vertical-nav–>
<?php echo $this->getChildHtml(‘topLeftLinks’) ?>
</div><!–End Of vertical-nav-container box base-mini–>
You could try replacing the function call
echo $this->drawItem($_category)
by
echo $this->drawOpenCategoryItem($_category)
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I would like to hide just a single category from my layered navigation. I have the "Show in Navigation" set to no but it still shows up on search results page. Is there a way to hide this?
To exclude a single category from layered navigation please follow the below steps. Note you could do this for any type of category listing.
Open /app/design/frontend/default/YOURTHEMENAME/template/catalog/navigation/left.phtml. If it does not exist, copy it from the base folder.
Find the code:
<dl id="narrow-by-list2">
<dt><?php echo $this->__('Category') ?></dt>
<dd>
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)
</li>
<?php endif; ?>
<?php endforeach ?>
</ol>
</dd>
</dl>
Add this if statement just inside the foreach loop (and dont forget to close it): <?php if ($_category->getId() != 22): ?>
The new code will look like this:
<dl id="narrow-by-list2">
<dt><?php echo $this->__('Category') ?></dt>
<dd>
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if ($_category->getId() != 22): ?> <!-- If statement here, replace category ID -->
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)
</li>
<?php endif; ?>
<?php endif; ?> <!-- End new If statement here -->
<?php endforeach ?>
</ol>
</dd>
</dl>
Replace the category ID with the ID of the category you wish to exclude.
To find out the ID of a category go to your Magento Admin panel and Categories, when you click to edit a category at the top left you will see it’s numerical ID.
I want do display selected category name and it subcategories on left side.
app\design\frontend\base\default\template\catalog\navigation\left.phtml
in left.phtml
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
<div class="block block-layered-nav">
<div class="block-title">
<strong><span><?php echo $this->__('Browse By') ?></span></strong>
</div>
<div class="block-content">
<dl id="narrow-by-list2">
<dt><?php echo $this->__('Category') ?>
</dt>
<dd>
<ol>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)
</li>
<?php endif; ?>
<?php endforeach ?>
</ol>
</dd>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list2')</script>
</div>
</div>
<?php endif; ?>
Using this code category listing can be viewed but instead of category name, 'CATEGORY' is displayed.How can i view current Category instead of just CATEGORY ?
Here you can get your current category
$_category = Mage::registry('current_category');