Today I am trying to add products like this:
– Category
– – SubCategory1
– – – Product 1
– – – Product 2
– – – Product 3
– – SubCategory2
– – – Product 1
– – – Product 2
– – – Product 3
My current magento version is ver. 1.9.0.1
I was searching about it on the internet I found this code but its giving me syntax error not sure why. Please check but this code tested on Magento ver 1.4 I am not sure if it will work for me or not.
protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
$isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
{
if (!$category->getIsActive()) {
return '';
}
$html = array();
// get all children
if (Mage::helper('catalog/category_flat')->isEnabled()) {
$children = (array)$category->getChildrenNodes();
$childrenCount = count($children);
} else {
$children = $category->getChildren();
$childrenCount = $children->count();
}
$hasChildren = ($children && $childrenCount);
// select active children
$activeChildren = array();
foreach ($children as $child) {
if ($child->getIsActive()) {
$activeChildren[] = $child;
}
}
$activeChildrenCount = count($activeChildren);
$hasActiveChildren = ($activeChildrenCount > 0);
// prepare list item html classes
$classes = array();
$classes[] = 'level' . $level;
$classes[] = 'nav-' . $this->_getItemPosition($level);
if ($this->isCategoryActive($category)) {
$classes[] = 'active';
}
$linkClass = '';
if ($isOutermost && $outermostItemClass) {
$classes[] = $outermostItemClass;
$linkClass = ' class="'.$outermostItemClass.'"';
}
if ($isFirst) {
$classes[] = 'first';
}
if ($isLast) {
$classes[] = 'last';
}
if ($hasActiveChildren) {
$classes[] = 'parent';
}
// prepare list item attributes
$attributes = array();
if (count($classes) > 0) {
$attributes['class'] = implode(' ', $classes);
}
if ($hasActiveChildren && !$noEventAttributes) {
$attributes['onmouseover'] = 'toggleMenu(this,1)';
$attributes['onmouseout'] = 'toggleMenu(this,0)';
}
// assemble list item with attributes
$htmlLi = '<li'; foreach="" ($attributes="" as="" $attrname=""> $attrValue) {
$htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
}
$htmlLi .= '>';
$html[] = $htmlLi;
$html[] = '<a href="'.$this->getCategoryUrl($category).'" '.$linkclass.'="">';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
// Grabbing the products for the category if it's level is 1
if ($level == 1) {
$catId = $category->getId();
$categorie = new Mage_Catalog_Model_Category();
$categorie->load($catId); // this is category id
$collection = $categorie->getProductCollection()->addAttributeToSort('name', 'asc');
$html[] = '<ul>';
foreach ($collection as $pc)
{
$p = new Mage_Catalog_Model_Product();
$p->load($pc->getId());
$data = $p->_data;
$html[] = '<li>'.$data['name'] .'</li>';
}
$html[] = "</ul>\n";
}
// Done
// render children
$htmlChildren = '';
$j = 0;
foreach ($activeChildren as $child) {
$htmlChildren .= $this->_renderCategoryMenuItemHtml(
$child,
($level + 1),
($j == $activeChildrenCount - 1),
($j == 0),
false,
$outermostItemClass,
$childrenWrapClass,
$noEventAttributes
);
$j++;
}
if (!empty($htmlChildren)) {
if ($childrenWrapClass) {
$html[] = '<div class="' . $childrenWrapClass . '">';
}
$html[] = '<ul class="level' . $level . '">';
$html[] = $htmlChildren;
$html[] = '</ul>';
if ($childrenWrapClass) {
$html[] = '</div>';
}
}
$html[] = '';
$html = implode("\n", $html);
return $html;
}
</li';>
Its giving syntax error on line:
$htmlLi = '<li'; foreach="" ($attributes="" as="" $attrname=""> $attrValue) {
and at the end of code:
</li';>
Related
This one is boggling me, because I think the code is right but something just isn't clicking, this is a code I picked up from Yanick Rochon and it works when the array is hard coded (in the commented out area) but when I push together the array from all of the pa_mymelp terms it doesn't behave as expected, it uses the entire line as an item vs multiple items per line, and does not create children either, wonder why.. thank you!:
<?php
$thearray = array();
$terms = get_terms("pa_mymelp");
foreach ( $terms as $term ) {
$categories = $term->name;
array_push($thearray, $categories);
}
$categoryLines = $thearray;
/*$categoryLines = array(
"Dodge>2002>Ram 3500>5.9 359cid L6 DIESEL OHV>Engine>Engine Oil Cooler",
"Dodge>2001>Ram 2500>5.9 359cid L6 DIESEL OHV>Engine>Engine Oil Cooler"
);*/
function buildCategoryTree($categoryLines, $separator) {
$catTree = array();
foreach ($categoryLines as $catLine) {
$path = explode($separator, $catLine);
$node = & $catTree;
foreach ($path as $cat) {
$cat = trim($cat);
if (!isset($node[$cat])) {
$node[$cat] = array();
}
$node = & $node[$cat];
}
}
return $catTree;
}
function displayCategoryTree($categoryTree, $indent = '') {
foreach ($categoryTree as $node => $children) {
echo $indent . $node . "\n";
displayCategoryTree($children, $indent . '|- ');
}
}
$categoryTree = buildCategoryTree($categoryLines, '>');
function displayHtmlCategoryTree($categoryTree, $id = null, $pathSeparator = '>', $parents = '') {
if (empty($categoryTree)) return '';
$str = '<ul' . (!empty($id) ? ' id="'.$id.'"' : '') . '>';
foreach ($categoryTree as $node => $children) {
$currentPath = $parents . (empty($parents) ? '' : $pathSeparator) . $node;
$str .= '<li title="' . $currentPath . '">' . $node .
displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath) .
'</li>';
}
$str .= '</ul>';
return $str;
}
echo displayHtmlCategoryTree($categoryTree, "test", '>');
?>
I have the below code to list the Array of terms, I am placing a comma between the terms if there are more than one term assigned to the post.
$terms = get_terms('my_term', $args);
if (!empty($terms) && !is_wp_error($terms)) {
$count = count($terms);
$i = 0;
$term_list = '<span>';
foreach ($terms as $term) {
$i++;
$term_list .= '#<span>' . $term->name . '</span>';
if ($count != $i) {
$term_list .= ', ';
} else {
$term_list .= '</span>';
}
}
}
Now, I would like place a & between the last two terms instead of a , if there are more than one term assigned to the post.
I think it is easier to solve it with an array.
$terms = get_terms('my_term', $args);
if (!empty($terms) && !is_wp_error($terms)) {
$term_array = [];
foreach ($terms as $term) {
$term_array[] = '#<span>' . $term->name . '</span>';
}
if(count($term_array) > 1){
$last = array_pop($term_array);
$term_list = '<span>' . implode(', ', $term_array) . '</span>';
$term_list .= ' & ' . $last;
} else {
$term_list = '<span>' . $term_array[0] . '</span>';
}
}
OR:
$terms = get_terms('my_term', $args);
if (!empty($terms) && !is_wp_error($terms)) {
$count = count($terms);
$i = 1;
$term_list = '<span>';
foreach ($terms as $term) {
$term_list .= '#<span>' . $term->name . '</span>';
if($i !== $count){
if($i === $count - 1){
$term_list .= ' & ';
} else {
$term_list .= ', ';
}
}
$i++;
}
$term_list .= '</span>';
}
Check if $count is equal to $i + 1:
if ($count != $i) {
if ($count == $i + 1)
$term_list .= '& ';
else
$term_list .= ', ';
} else {
$term_list .= '</span>';
}
That should do it.
You could check if the $count is equal to $i.
$i = 1;
if ($count != $i) {
$term_list .= ', ';
} else if ($count == $i) {
$term_list .= '& ';
} else {
$term_list .= '</span>';
}
Find the last element of the array, then just check each item within your loop against lastelement, then do your 'magic' ;).
Example:
$array = array('a' => 1,'b' => 2,'c' => 3);
$lastElement = end($array);
foreach($array as $k => $v) {
echo $v . '<br/>';
if($v == $lastElement) {
// 'Now you know that $v is the last element, do something';
}
}
I'm currently working on a Magento installation with multiple websites and store views. I'm attempting to redesign one of the four sub websites.
There has been a custom module added which appears to rewrite/extend the default top navigation menu to automatically include CMS pages and add a banner slot to the menu. The problem is that I want to restore the default Magento top menu (i.e. no CMS pages) for a single website view.
I've tried disabling the module inside System -> Config -> Advanced -> Advanced for that website, however this seems to make the entire top navigation disappear. I believe the function I want to remove is this:
<?php
/**
* extend functions from navigation.php
*
*/
class Fvzzy_Category_Block_Navigation extends Mage_Catalog_Block_Navigation
{
protected $_menus;
protected function _getCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
$isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
{
if (!$category->getIsActive()) {
return '';
}
$html = array();
// get all children
if (Mage::helper('catalog/category_flat')->isEnabled()) {
$children = (array)$category->getChildrenNodes();
$childrenCount = count($children);
} else {
$children = $category->getChildren();
$childrenCount = $children->count();
}
$hasChildren = ($children && $childrenCount);
// select active children
$activeChildren = array();
foreach ($children as $child) {
if ($child->getIsActive()) {
$activeChildren[] = $child;
}
}
$activeChildrenCount = count($activeChildren);
$hasActiveChildren = ($activeChildrenCount > 0);
// prepare list item html classes
$classes = array();
$classes[] = 'level' . $level;
//$classes[] = 'nav-' . $this->_getItemPosition($level);
$text = preg_replace('/[^A-Za-z0-9]/i', '-', strtolower($category->getName()));
//$classes[] = $category->;
if ($this->isCategoryActive($category)) {
$classes[] = 'active';
}
$linkClass = '';
if ($isOutermost && $outermostItemClass) {
$classes[] = $outermostItemClass;
$linkClass = ' class="'.$outermostItemClass.'"';
}
if ($isFirst) {
$classes[] = 'first';
}
if ($isLast) {
//$classes[] = 'last';
}
if ($hasActiveChildren) {
$classes[] = 'parent';
}
if(strtolower($text) == 'sale') $classes[] = strtolower($text);
// prepare list item attributes
$attributes = array();
if (count($classes) > 0) {
$attributes['class'] = implode(' ', $classes);
}
if ($hasActiveChildren && !$noEventAttributes) {
$attributes['onmouseover'] = 'toggleMenu(this,1)';
$attributes['onmouseout'] = 'toggleMenu(this,0)';
}
// assemble list item with attributes
$htmlLi = '<li';
foreach ($attributes as $attrName => $attrValue) {
$htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
}
$htmlLi .= '>';
$html[] = $htmlLi;
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
// render children
$htmlChildren = '';
$j = 0;
foreach ($activeChildren as $child) {
$htmlChildren .= $this->_getCategoryMenuItemHtml(
$child,
($level + 1),
($j == $activeChildrenCount - 1),
($j == 0),
false,
$outermostItemClass,
$childrenWrapClass,
$noEventAttributes
);
$j++;
}
$promotion = $this->addPromotions($category->getId());
if (!empty($htmlChildren)) {
if ($childrenWrapClass) {
$html[] = '<div class="' . $childrenWrapClass . '">';
}
$html[] = '<ul class="level' . $level . '"><div class="menu-text">';
if($promotion) $html[] = '';
$html[] = $htmlChildren;
if($promotion) $html[] = '';
if($promotion) $html[] = '</div><li class="level1 parent menu-promotion">'.$promotion.'</li>';
$html[] = '</ul>';
if ($childrenWrapClass) {
$html[] = '</div>';
}
}
$html[] = '</li>';
$html = implode("\n", $html);
return $html;
}
public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
{
$activeCategories = array();
foreach ($this->getStoreCategories() as $child) {
if ($child->getIsActive()) {
$activeCategories[] = $child;
}
}
$activeCategoriesCount = count($activeCategories);
$hasActiveCategoriesCount = ($activeCategoriesCount > 0);
if (!$hasActiveCategoriesCount) {
return '';
}
$html = '';
$j = 0;
foreach ($activeCategories as $category) {
$html .= $this->_getCategoryMenuItemHtml(
$category,
$level,
($j == $activeCategoriesCount - 1),
($j == 0),
true,
$outermostItemClass,
$childrenWrapClass,
true
);
$j++;
}
//main store
if(Mage::app()->getStore()) $html .= $this->addMenu($childrenWrapClass,Mage::app()->getStore());
return $html;
}
protected function addPromotions($id = 0){
if($id){
$base = Mage::getBaseUrl('media',true).'promotion_box_images/';
$file_base = Mage::getBaseDir('media').'/promotion_box_images';
$html = ''; $img = '';
$promotion = Mage::getModel('promotion/box')->getCollection()->addFieldToFilter('menu_ids',array(
array('like'=>$id),
array('like'=>$id.',%'),
array('like'=>'%,'.$id.',%'),
array('like'=>'%,'.$id)))->setOrder('position')->getFirstItem(); //default is desc
if($promotion && $promotion->getId()){
$type=$promotion->getDisplayType();
$link = ''; $img = ''; $content = ''; $width = '';
if(file_exists($file_base.'/'.$promotion->getImage())){
list($width) = getimagesize($file_base.'/'.$promotion->getImage());
}
if($type!=null){
switch($type){
case 0:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if(trim($promotion->getLink())!='') $link = $promotion->getLink();
break;
case 1:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if($promotion->getCategoryId()){
$c = Mage::getModel('catalog/category')->load($promotion->getCategoryId());
if($c->getId() && $c->getIsActive()) $link = Mage::helper('catalog/category')->getCategoryUrl($c);
}
break;
case 2:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if($promotion->getProductSku()){
$id = Mage::getModel('catalog/product')->getIdBySku($promotion->getProductSku());
$p = Mage::getModel('catalog/product')->load($id);
if($p->getId()) $link = $p->getProductUrl();
}
break;
case 3:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if($promotion->getPageId()){
$_link = Mage::Helper('cms/page')->getPageUrl($promotion->getPageId());
if($_link != ''){
$nodes = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection()
->addFieldToFilter('page_id', array('eq' => $promotion->getPageId()));
foreach($nodes as $_node){
$link = $_node->getRequestUrl();
}
if(!$link){$link = Mage::Helper('cms/page')->getPageUrl($promotion->getPageId());}
}
}
break;
case 4:
$content = $promotion->getContent();
break;
}
}
if($img && $link){
$html = ''.$img.'';
}
elseif($img){
$html = ''.$img.'';
}
else{
$html = $content;
}
}
return $html;
}else{return false;}
}
//$menu = array( array('label'=>'News','href'=>'blog','module'=>'blog','sub'=>array(array('label'=>'submenu','href'=>'blog2','module'=>'blog'))) );
public function setOtherMenu($menu){
$this->_menus = $menu;
}
public function addMenu( $childrenWrapClass = '', $store ) {
$this->_node_ids = array();
$current_module = $this->getRequest()->getModuleName();
$page_id = $this->getRequest()->getParam('page_id');
// get all the cms page nodes not folder or containers
$nodes = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection()
->joinMetaData()
->addFieldToFilter( 'level', 1 )
//->addFieldToFilter( 'menu_visibility', 1 )
->addFieldToFilter( 'main_table.page_id', array( 'notnull' => true ))
->setOrder('sort_order','ASC');
if ( $store instanceof Mage_Core_Model_Store ) {
$store = $store->getId();
$storeIds = array( 0, $store );
$nodes->getSelect()
->joinLeft( array( 'cs' => 'cms_page_store' ), 'main_table.page_id=cs.page_id', array( 'cs.store_id' ) )
->where( 'cs.store_id IN ('. implode( ',', $storeIds ) .')' ); // not the best sql here but it works as store id will be one at a time.
}
$active_node = Mage::registry('current_cms_hierarchy_node');
$active_node_ids = array();
if($active_node){
$xpath = $active_node->getXpath();
$all = explode( '/', $xpath );
foreach ( $all as $index ) $active_node_ids[] = $index;
}
$this->_node_ids = $active_node_ids;
if ( $this->_menus ) {
$menus = $this->_menus;
}
else {
$menus = array();
}
$links = '';
foreach ( $nodes as $node ) {
$tree = $node->setCollectActivePagesOnly(true)
->setCollectIncludedPagesOnly(true)
->setTreeMaxDepth(0)
->setTreeIsBrief(1) //this way it won't show the container link
->getTreeSlice(0, 1); //up to tree top and down to one level
$links .= $this->drawCmsMenu($tree,0,1,$childrenWrapClass);
}
foreach ( $menus as $menu ) {
if(isset($menu['sub'])) $has_sub_class = ' parent'; else $has_sub_class = '';
if (isset($menu['module']) && $current_module == $menu['module'] ) {
$links .= '<li class="level0 level-top active'.$has_sub_class.'">';
}
else {
$links .= '<li class="level0 level-top'.$has_sub_class.'">';
}
$html = '';
if(isset($menu['sub']) && is_array($menu['sub'])){
$submenu = $menu['sub'];
$html .= '<div class="'.$childrenWrapClass.'"><ul class="level0">';
foreach($submenu as $sub){
if(is_array($sub) &&isset($sub['href']) && isset($sub['label']))
//2012-11-20 AU TEAM coding for change <h2> -> <span>
$html .= '<li class="level1"><span>'.$sub['label'].'</span></li>';
}
$html .= '</div>';
}
if(isset($menu['href']) && isset($menu['label']))
$links .= '<a class="level-top" href="'.Mage::getBaseUrl().$menu['href'].'"><span>'.$menu['label'].'</span></a>';
//add submenu
if($html != '') $links .= $html;
$links .= '</li>';
}
return $links;
}
/* Add Maximal Depth filter
* 2012.04.11 by Bruce
*/
public function drawCmsMenu( array $tree, $parentNodeId = 0, $level = 0, $childrenWrapClass = '' ) {
$html = '';
if ( !isset($tree[$parentNodeId ])) return $html;
foreach ( $tree[ $parentNodeId ] as $nodeId => $node ) {
/* #var $node Enterprise_Cms_Model_Hierarchy_Node */
$nested = $this->drawCmsMenu( $tree, $nodeId, $node->getLevel()+1, $childrenWrapClass);
$hasChildren = ( $nested != '' );
// set style classes
$class = array();
$class[] = 'level'. ( $level - 1 );
if ( $level - 1 == 0 )
$class[] = 'level-top';
if ( $this->_node_ids && in_array( $node->getNodeId(), $this->_node_ids ) )
$class[] = 'active';
if ( $hasChildren )
$class[] = 'parent';
// li wrapper header
$html .= '<li class="'. implode( ' ', $class ) .'">';
$html .= $this->_getNodeLabel( $node, $level - 1 );
// div wrapper header
if ( $hasChildren ){
if ( $childrenWrapClass ) $html .= '<div class="'. $childrenWrapClass .'">';
$html .= '<ul class="level'. ( $level - 1 ) .'">';
}
// children
$html .= $nested;
// div wrapper footer
if ( $hasChildren ) {
$html.='</ul>';
if ( $childrenWrapClass ) $html .= '</div>';
}
// li wrapper footer
$html .= '</li>';
}
return $html;
}
protected function _getNodeLabel($node,$level)
{
if($level==0) $class = 'level-top'; else $class = '';
if($node->getPageTitle()) return '<a class="'.$class.'" href="'.$node->getUrl().'"><span>'.$node->getPageTitle().'</span></a>';
else return '<a class="'.$class.'" href="'.$node->getUrl().'"><span>'.$node->getLabel().'</span></a>';
}
}
I know that the config file for this module looks like the following:
<?xml version="1.0"?>
<config>
<modules>
<Fvzzy_Category>
<version>0.1.0</version>
</Fvzzy_Category>
</modules>
<global>
<blocks>
<fvzzy_category>
<class>Fvzzy_Category_Block</class>
</fvzzy_category>
<catalog>
<rewrite>
<navigation>Fvzzy_Category_Block_Navigation</navigation>
</rewrite>
</catalog>
</blocks>
<resources>
<fvzzy_category_setup>
<setup>
<module>Fvzzy_Category</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
</fvzzy_category_setup>
</resources>
</global>
</config>
And that if I'm able to remove the following part from that file, I get my intended outcome:
<catalog>
<rewrite>
<navigation>Fvzzy_Category_Block_Navigation</navigation>
</rewrite>
</catalog>
The problem is, I just want to get the top navigation menu function restored back to normal for one website view.
Is there any way to overwrite that navigation rewrite event for one store/website view?
If not, is it possible to disable this module without it disabling my whole top menu for that website view?
I've tried completely duplicating the module, disabling the original one and enabling the new one in my desired website but that also appears to be making the top menu disappear entirely.
I'm really not sure what else to do with this - if anyone is able to help it would be hugely appreciated.
Thanks so much.
if you want this extension still for your Magento application, then disabling the extension will not produce expecting result.
In this case, you can do two things
Overwrite this custom extension with your module and make changes according to your need
comment out the config section that defines that core block overwrite. You have already tried it and worked it
There is another alternative. You can do a condition checking inside the rewrite block class. This condition check somewhat look like this.
if(Mage::app()->getStore()->getCode()== 'website_code_for_sub_site'){
//execute existing code
}
This method ensure that, the method that defined inside the rewrite block class will get executed only when the site with a code website_code_for_sub_site is viewing.
Hope that helps
I have a problem with navigation in Magento. The child categories displays in a funny way, which i do not want them too. Please see the following screenshot:
http://tinypic.com/r/2l97byp/8
In upper left, the childs are shown in the menu. However i do not want them to expanding into a new block, but simply show below their parrent category as seen in this picture:
tinypic.com/r/16l0kk3/8
I have two sets of code for this, however i do not know what to edit to get the desired result. I have tried to get it to work, but with no luck.
The two code parts:
top.phtml:
<?php
/**
* Top menu for store
*
* #see Olegnax_Navigation_Block_Navigation
*/
?>
<?php
/**
* $this->renderCategoriesMenuHtml() supports optional arguments:
* int Level number for list item class to start from
* string Extra class of outermost list items
* string If specified wraps children list in div with this class
*/
?>
<!-- navigation BOF -->
<?php $_menu = $this->renderCategoriesMenuHtml(0, 'level-top', 'sub-wrapper' ) ?>
<?php if($_menu): ?>
<nav class="olegnax">
<ul id="nav">
<?php if (Mage::getStoreConfig('celebritysettings/celebritysettings_header/navigation_home')): ?>
<li class="level0 level-top">
<span><?php echo $this->__('Home'); ?></span>
</li>
<?php endif; ?>
<?php
echo $_menu;
$custom_tab = Mage::getModel('cms/block')->load('celebrity_navigation_block');
if($custom_tab->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="#" class="level-top">
<span>'.$custom_tab->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block')->toHtml().'</div>
</li>';
}
?>
</ul>
</nav>
<?php endif ?>
<!-- navigation EOF -->
And Navigation.phtml:
<?php
/**
* #version 1.0 12.0.2012
* #author Olegnax http://www.olegnax.com <mail#olegnax.com>
* #copyright Copyright (C) 2010 - 2012 Olegnax
*/
class Olegnax_Navigation_Block_Navigation extends Mage_Catalog_Block_Navigation
{
/**
* columns html
*
* #var array
*/
protected $_columnHtml;
/**
* Render category to html
*
* #param Mage_Catalog_Model_Category $category
* #param int Nesting level number
* #param boolean Whether ot not this item is last, affects list item class
* #param boolean Whether ot not this item is first, affects list item class
* #param boolean Whether ot not this item is outermost, affects list item class
* #param string Extra class of outermost list items
* #param string If specified wraps children list in div with this class
* #param boolean Whether ot not to add on* attributes to list item
* #return string
*/
protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
$isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
{
if (!$category->getIsActive()) {
return '';
}
$html = array();
// get all children
if (Mage::helper('catalog/category_flat')->isEnabled()) {
$children = (array)$category->getChildrenNodes();
$childrenCount = count($children);
} else {
$children = $category->getChildren();
$childrenCount = $children->count();
}
$hasChildren = ($children && $childrenCount);
// select active children
$activeChildren = array();
foreach ($children as $child) {
if ($child->getIsActive()) {
$activeChildren[] = $child;
}
}
$activeChildrenCount = count($activeChildren);
$hasActiveChildren = ($activeChildrenCount > 0);
// prepare list item html classes
$classes = array();
$classes[] = 'level' . $level;
$classes[] = 'nav-' . $this->_getItemPosition($level);
if ($this->isCategoryActive($category)) {
$classes[] = 'active';
}
$linkClass = '';
if ($isOutermost && $outermostItemClass) {
$classes[] = $outermostItemClass;
$linkClass = ' class="'.$outermostItemClass.'"';
}
if ($isFirst) {
$classes[] = 'first';
}
if ($isLast) {
$classes[] = 'last';
}
if ($hasActiveChildren) {
$classes[] = 'parent';
}
// prepare list item attributes
$attributes = array();
if (count($classes) > 0) {
$attributes['class'] = implode(' ', $classes);
}
if ($hasActiveChildren && !$noEventAttributes) {
$attributes['onmouseover'] = 'toggleMenu(this,1)';
$attributes['onmouseout'] = 'toggleMenu(this,0)';
}
// assemble list item with attributes
$htmlLi = '<li';
foreach ($attributes as $attrName => $attrValue) {
$htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
}
$htmlLi .= '>';
$html[] = $htmlLi;
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
if ( $level == 0 ) {
//get category description
$ca = Mage::getModel('catalog/category')->load($category->getId());
$description = $ca->getDescription();
if ( empty($description) || !Mage::getStoreConfig('celebritysettings/celebritysettings_navigation/show_description') ) {
$columns = 4;
} else {
$columns = 2;
}
$columnItemsNum = array_fill(0, $columns, floor($activeChildrenCount / $columns));
if ( $activeChildrenCount % $columns > 0 ) {
for ($i = 0; $i < ($activeChildrenCount % $columns); $i++ ) {
$columnItemsNum[$i]++;
}
}
$this->_columnHtml = array();
}
// render children
$htmlChildren = '';
$j = 0; //child index
$i = 0; //column index
$itemsCount = $columnItemsNum[$i];
foreach ($activeChildren as $child) {
if ( $level == 0 ) {
$isLast = (($j+1) == $itemsCount || $j == $activeChildrenCount - 1);
if ( $isLast ) {
$i++;
$itemsCount += $columnItemsNum[$i];
}
} else {
$isLast = ($j == $activeChildrenCount - 1);
}
$childHtml = $this->_renderCategoryMenuItemHtml(
$child,
($level + 1),
$isLast,
($j == 0),
false,
$outermostItemClass,
$childrenWrapClass,
$noEventAttributes
);
if ( $level == 0 ) {
$this->_columnHtml[] = $childHtml;
} else {
$htmlChildren .= $childHtml;
}
$j++;
}
if ( $level == 0 && $this->_columnHtml ) {
$i = 0;
foreach ( $columnItemsNum as $columnNum ) {
$chunk = array_slice($this->_columnHtml, $i, $columnNum);
$i += $columnNum;
$htmlChildren .= '<li '.(count($this->_columnHtml) == $i ? 'class="last"' : '').'><ol>';
foreach ( $chunk as $item ) {
$htmlChildren .= $item;
}
$htmlChildren .= '</ol></li>';
}
}
if ( !empty($description) && !empty($htmlChildren) && Mage::getStoreConfig('celebritysettings/celebritysettings_navigation/show_description') ) {
$htmlChildren .= '<li class="menu-category-description clearfix">'.$description;
if ( Mage::getStoreConfig('celebritysettings/celebritysettings_navigation/show_learn_more') ) {
$htmlChildren .= '<p><button class="button" onclick="window.location=\''.$this->getCategoryUrl($category).'\'"><span><span>'.$this->__('learn more').'</span></span></button></p>';
}
$htmlChildren .= '</li>';
}
if (!empty($htmlChildren)) {
if ($childrenWrapClass) {
$html[] = '<div class="' . $childrenWrapClass . '">';
}
$html[] = '<ul class="level' . $level . '">';
$html[] = $htmlChildren;
$html[] = '</ul>';
if ($childrenWrapClass) {
$html[] = '</div>';
}
}
$html[] = '</li>';
$html = implode("\n", $html);
return $html;
} }
I hope someone can see what to do in order to get the effect on the image :)
Best Regards,
Patrick
Well, Alan Storm is correct but for what it is worth, to answer your question 'which file do I edit', the file you edit is Navigation.php. I say this because top.phtml has the line echo $_menu;. Anyway, the code in protected function _renderCategoryMenuItemHtml() is perhaps a little convoluted and when I read the code and think about what you are trying to achieve I think you should either:
a) Try commenting out the lines:
if ($hasActiveChildren && !$noEventAttributes) {
$attributes['onmouseover'] = 'toggleMenu(this,1)';
$attributes['onmouseout'] = 'toggleMenu(this,0)';
}
in order to remove the mouseover interaction and then see if you can hack the CSS to get the layout you desire
or
b) Throw away all the code in protected function _renderCategoryMenuItemHtml() and start again: rewrite it to display the HTML structure you require using elements of the existing code.
I what to put a span element for $term['nodes']
I have tried to put after bracket and between but nothing works for me
if (isset($term['nodes'])) {
$term['name'] = $term['name'] . ' (' . $term['nodes'] . ')';
}
here is the all functin
function bootstrap_taxonomy_menu_block($variables) {
$tree = $variables['items'];
$config = $variables['config'];
$num_items = count($tree);
$i = 0;
$output = '<ul class="nav nav-pills nav-stacked">';
foreach ($tree as $tid => $term) {
$i++;
// Add classes.
$attributes = array();
if ($i == 1) {
$attributes['class'][] = '';
}
if ($i == $num_items) {
$attributes['class'][] = '';
}
if ($term['active_trail'] == '1') {
$attributes['class'][] = 'active-trail';
}
if ($term['active_trail'] == '2') {
$attributes['class'][] = 'active';
}
// Alter link text if we have to display the nodes attached.
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
}
// Set alias option to true so we don't have to query for the alias every
// time, as this is cached anyway.
$output .= '<li' . drupal_attributes($attributes) . '>' . l($term['name'], $term['path'], $options = array('alias' => TRUE));
if (!empty($term['children'])) {
$output .= theme('taxonomy_menu_block__' . $config['delta'], (array('items' => $term['children'], 'config' => $config)));
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
i what this for the bootstrap cdn class , i have move the function on template.php , of drupal theme , but the span element is in plain text in browser
Try this:
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
echo $term['name']; // To see the output
}