Magento - Adding layered navigation to custom page - magento

I'm working on a magento module that shows particular product collections under a new controller and front-name.
Some of these collections get big, so I'd like to add layered navigation to the side of the page. (And hey, pagination and sort while we're at it.)
I can add the layered navigation block with
<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav" template="landing/layer.phtml"/>
</reference>
What I get with that is the layered navigation as applied to the whole catalog, with categories broken, and no interface with the on-page product collection.
How would I go about wiring up the layered navigation (and hey, pagination and sort) to this custom product collection?

No answers were forthcoming here, and I changed direction and never completed this path of development. Figured I'd post what I'd learned.
The approach above is sound. The work basically entails recreating the functionality on the catalog or catalogsearch modules. You'll have to subclass the models and blocks of catalog, modifying the product collection and current category.
This post heads vaguely in the right direction, but doesn't get there.
http://www.webdesign-gm.co.uk/news/layered-navigation-on-home-page-or-any-cms-page-magento.php
If you make more headway on this, feel free to post.

I've had a similar request from a client to include specific filterable attributes under a mega menu.
Using a static block i've added this line to everywhere i needed a listing of attributes for specific category
{{block type="core/template" attribute_code="age_specific" category_id="12" template="megamenu-custom-filter-list.phtml"}}
And then the "megamenu-custom-filter-list.phtml"
<?php if($this->getCategoryId() && is_numeric($this->getCategoryId()) && $this->getAttributeCode()): ?>
<?php
$visible_items = 12;
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$attrCode = $this->getAttributeCode();
unset($layer);
$layer = Mage::getModel("catalog/layer");
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
foreach ($attributes as $attribute):
if($attribute->getAttributeCode() != $attrCode){
continue;
}
if ($attribute->getAttributeCode() == 'price') {
$filterBlockName = 'catalog/layer_filter_price';
} elseif ($attribute->getBackendType() == 'decimal') {
$filterBlockName = 'catalog/layer_filter_decimal';
} else {
$filterBlockName = 'catalog/layer_filter_attribute';
}
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
?>
<div>
<h4 class="menu-block-title"><?php echo $this->__($attribute->getFrontendLabel()) ?></h4>
<ul class="menu-attr-list">
<?php $counting = 1; ?>
<?php foreach($result->getItems() as $option): ?>
<?php $optionUrl = $category->getUrl() . "?" . $attribute->getAttributeCode() . "=" . $option->getValue(); ?>
<?php if(!$option->getCount()){ continue; } ?>
<li class="<?php echo ($counting >= $visible_items+1)?"visible-on-showmore":"" ?>" style="list-style: none;">
<a class="cube" href="<?php echo $optionUrl ?>">
<?php echo $option->getLabel() ?> <?php /* (<?php echo $option->getCount() ?>) */ ?>
</a>
</li>
<?php $counting++; ?>
<?php endforeach; ?>
<?php if($counting >= $visible_items+1): ?>
<li class="show-more-menuitem">
<a href="javascript:void(0)" onclick="showMoreMenuItems(this); return false;" class="">
<?php echo $this->__('Visa fler') ?>
</a>
</li>
<?php endif; ?>
</ul>
</div>
<?php endoreach; ?>
<?php endif; ?>
Of course, this can be extended to allow all attributes to be shown and not limit them to 12 (as i did here). Although, i've not build a feature to show this for a custom collection, but i believe that following the "catalog/layer" model and see where the collection was loaded, you can see how to overwrite it and load your own custom collection.

Related

Magento Static block now showing up in CMS Page

Hy ! I'm trying to display the categories on a cms page.I've tried all the solution on the web but none is working for me. The last one i've tried is this.
1.I've added this code in content tab of my cms page :
{{block type="catalog/navigation" template="catalog/category/list.phtml"}}
2.I've created the list.phtml and put the file on app/design/theme-name/template/catalog/category.
Here is the code pf my file
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $open = $this->isCategoryActive($_category); ?>
<?php
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
if ($immagine = $this->getCurrentCategory()->getImageUrl()):
?>
<div class="catalog-image">
<div>
<a href="<?php echo $this->getCategoryUrl($_category)?>">
<img src="<?php echo $immagine ?>" alt="<?php echo $this->htmlEscape($this->getCurrentCategory()->getName()) ?>" width="313" height="151" />
</a>
</div>
<div class="left"><h2><?php echo $_category->getName()?></h2></div>
</div>
<?php endif; ?>
<?php endforeach; ?>
What I'm doing wrong ? Thanks !
Add this code in content tab of your cms page:
{{block type="core/template" template="page/categories-list.phtml"}}
Create a file in the theme such "categories-list.phtml"
path: app/design/theme-name/template/page/categories-list.phtml
In this file, write the following code in order to get a collection of all the categories:
<?php
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter();
?>
If you want only some categories of parent, use the following code:
<?php
$parent_category_id = 5; // this is ID of parent category
$categories = Mage::getModel('catalog/category')->getCategories($parent_category_id);
?>
To display categories on the screen, use loop, look below:
<?php foreach ($categories as $category): ?>
<p><?php echo $category->getImageUrl(); ?></p>
<p><?php echo $category->getName(); ?></p>
<?php endforeach; ?>
If you use the categories of the parent category to display the image, use the following code:
Mage::getModel('catalog/category')->load($category->getId())->getImageUrl();
Create a folder under catalog with name navigation and put your list.phtml file there it will work.
{{block type="core/template" template="catalog/navigation/list.phtml" category_id="507" }}
Category Display Mode shuould be in static block in display setting.

how to give link to each attributes of a manufacturer

I have added 4 to 5 attributes in manufacture and shown in right column of front end.Now i want to link the each attributes of manufacture. if i will click an attribute of a manufacturer, it will show all the products which contain that arribute/brand.
if anyone knows this, please help me out.
thanks!
I have shown the attribute name in front end by the below code
<?php
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'Manufacturer');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
if(count( $options)>0){
?>
<div class="title_box">Manufacturers</div>
<?php $i=1;?>
<ul class="left_menu">
<?php
foreach($options as $eachval){
?>
<?php if($i%2==0){ ?>
<li class="even"><?php echo $eachval['label']?></li>
<?php } else { ?>
<li class="odd"><?php echo $eachval['label']?></li>
<?php } $i++; ?>
<?php } ?>
</ul>
<?php } } ?>
I have made one page manu.phtml in catalog/product page and put the following above code now how to give link to that arribute...........please describe briefly
in href link,what i have to write so that when i will click on any attribute it will show all products associated to that attribute/brand.
There is always the option of creating a new module with a custom controller that would list the products from a specified brand, but that is a painful process even if it's the clean way.
Here is a simple version if you don't mind the ugly urls.
The main idea is to link your brand names to the advanced search page with a specific brand filled in.
You can get the url like this:
$url = Mage::getUrl('catalogsearch/advanced/result', array('_query'=>'brand='.$value->getId()))
You just need now to get the id of the specific brand ($value->getId()), but if you can get the name you can get the id also.
And don't forget to specify that the brand attribute is used in advanced search. You can do that by editing the attribute in the backend.
[EDIT]
Make your ul element look like this:
<ul class="left_menu">
<?php
foreach($options as $eachval){
$url = Mage::getUrl('catalogsearch/advanced/result', array('_query'=>'Manufacturer='.$eachval['value']));
?>
<?php if($i%2==0){ ?>
<li class="even"><?php echo $eachval['label']?></li>
<?php } else { ?>
<li class="odd"><?php echo $eachval['label']?></li>
<?php } $i++; ?>
<?php } ?>
</ul>
Small tip off topic. You can avoid duplication of the li elements in your code like this
<ul class="left_menu">
<?php
foreach($options as $eachval){
$url = Mage::getUrl('catalogsearch/advanced/result', array('_query'=>'Manufacturer='.$eachval['value']));
?>
<li class="<?php echo ($i%2 == 0) ? 'even':'odd';?>"><?php echo $eachval['label']?></li>
<?php } $i++; ?>
<?php } ?>
</ul>

Magento: textbox instead of multi select in layered navigation

Is there a possibility in Magento to have a multi-select attribute, for which I would use a textbox in layered navigation instead of showing all items from multi-select?
I have an attribute where I will have hundreds of options and need to use it in layered navigation.
When customer uses invalid value, an error should show up.
Edit: After the help from FlorinelChis I have folowing code in filter.phtml:
<ol>
<?php foreach ($this->getItems() as $_item): ?>
<?php
$attributeModel = $this->getAttributeModel();
$attribute_code = $attributeModel->getAttributeCode();
?>
<li>
<?php if ($attribute_code != 'available_zip'): ?>
<?php if ($_item->getCount() > 0): ?>
<?php echo $_item->getLabel() ?>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach ?>
</ol>
<?php
if ($attribute_code == 'available_zip'):
$cat = Mage::registry('current_category')->getUrlPath() ;
$url = Mage::getBaseUrl();
/*$sendUrl = $this->urlEscape($_item->getUrl()).'+'.$url.$cat.'?'.$attribute_code.'='.$_item->getValue();*/
echo '<form action="" method="get">';
echo '<input type="text" size="5" maxlength="5" name="'.$attribute_code.'" />';
echo '<button type="submit">OK</button>';
echo '</form>';
endif; ?>
I have one more thing now:
how to send the form with attribute id instead of value?
First, let's find out how Magento displays the filters in left hand navigation
1) Enable Template Path Hints:
and here is the result:
2) Let's take a look at app/design/frontend/base/default/template/catalog/layer/filter.phtml (you should copy this file in your theme folder structure)
3) The block ($this in the template file is an instance of Mage_Catalog_Block_Layer_Filter_Attribute which extends Mage_Catalog_Block_Layer_Filter_Abstract)
4) You can see that in Mage_Catalog_Block_Layer_Filter_Abstract a getName() method exists, so you could rely on this function to identify when you attribute is displayed. Don't! The label can change and your code won't be useful any more.
Back to our template file, you can get the attribute_code (which is reliable)
//$this is instance of Mage_Catalog_Block_Layer_Filter_Attribute
$attributeModel = $this->getAttributeModel();
$attribute_code = $attributeModel->getAttributeCode();
5) On your template you can have a check based on attribute code, so you display either the standard list or your custom html code with a textarea instead of a huge list.

How to add submenu on hover effect in left navigation in magento?

I have vertnav/left.phtml file code,
<div class="vertnav-container">
<div class="">
<h4 class="no-display"><?php echo $this->__('Category Navigation:') ?></h4>
<?php $store_categories = $this->toLinearArray($this->getStoreCategories()) ?>
<?php if ($count = count($store_categories)): ?>
<ul id="vertnav">
<?php endif; ?>
<?php foreach ($store_categories as $i => $_category): ?><?php $class = array() ?>
<?php if ($count == 1): ?>
<?php $class[] = 'only' ?>
<?php elseif (! $i): ?>
<?php $class[] = 'first' ?>
<?php elseif ($i == $count-1): ?>
<?php $class[] = 'last' ?>
<?php if (isset($store_categories[$i+1]) && $this->isCategoryActive($store_categories[$i+1])) $class[] = 'prev'; ?>
<?php if (isset($store_categories[$i-1]) && $this->isCategoryActive($store_categories[$i-1])) $class[] = 'next'; ?>
<?php echo $this->drawOpenCategoryItem($_category, 0, $class) ?>
<?php endforeach ?>
<?php if ($count): ?>
</ul>
<?php endif; ?>
</div>
</div>
and set System > Configuration > Catalog > Category Vertical Navigation to 2 as per my requirement, but now on mouseover on that displayed category subcategories should be shown
so how can i do customization to that and add hover effect code to this?
Please help me
If you take a closer look at the drawOpenCategoryItem of the Mage_Catalog_Block_Navigation you might notice that the method does a check whether the given category is part of the current category path. So only when this check returns true, the children categories of this category will be rendered. For other categories the script will not go into that part of the code.
This sounds to be case if I understand your question correctly.
if (in_array($category->getId(), $this->getCurrentCategoryPath())){
$children = $category->getChildren();
$hasChildren = $children && $children->count();
if ($hasChildren) {
$htmlChildren = '';
foreach ($children as $child) {
$htmlChildren.= $this->drawOpenCategoryItem($child);
}
if (!empty($htmlChildren)) {
$html.= '<ul>'."\n"
.$htmlChildren
.'</ul>';
}
}
}
Additional to this information. The drawOpenCategoryItem() is never actually called upon in the whole PHP codebase.
So to have these rollover effects you need to have code that generates the full tree structure, or at least a big enough part of it. Regarding the System > Configuration > Catalog > Category Vertical Navigation. I guess you customized that yourself?
To give you a few pointers. You might want to have a look at the following methods. These are used for the rendering of the top menu and are actually doing the thing you are planning to implement.
Mage_Catalog_Block_Navigation::renderCategoriesMenuHtml()
Mage_Catalog_Block_Navigation::_renderCategoryMenuItemHtml()
Hopes this helps you getting things underway.

Magento view related products in list.phtml

I have the following requirement;
I want to show a products configured related products on the category listing page (list.phtml).
I figured I could make some call on the list.phtml inbetween the for each loop for each product using the [b]$_product[/b] variable but I can't seem to populate the relatedProductCollection
sorry new to all this is it even possible.
<?php foreach ($_productCollection as[b] $_product[/b]):?>
<li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?>last<?php endif; ?>" >
<div class="product-shop">
<div class="f-fix">
<?php $product->getRelatedProductCollection(); ?>
</li>
<?php endforeach; ?>
$product has been used to call the getRelatedProductCollection function.
but,
In that foreach loop you have taken it as $_product.
Can you see the difference ??
Underscore is missing. This is taken from your code. Make the variable same in both the places and try.

Resources