sort collection of category by name - magento

I have custom links in menu that shows categories in drop-down.
For that I made a file in catalog/navigation->mainmenu.phtml
(a custom file)
Now, I want to show categories after sorting it by name. please help me how can I sort collection of categories by name. I already set order of category in admin. But in front-end it show unsorted.
Code is:-
<?php $defaultcategory= Mage::app()->getStore()->getRootCategoryId();?>
<?php $mainchildren = Mage::getModel('catalog/category')->getCategories($defaultcategory);?>
<ul>
<?php foreach ($mainchildren as $subcategory) : ?> <?php // 2 level ?>
<?php if($subcategory->getIsActive()):?>
<li id="show_subcat" class="<?php if($i==1): echo 'first'; endif; ?>" >
<?php $childid=$subcategory->getId();?>
<?php $subchild = Mage::getModel('catalog/category')->getCategories($childid);?>
<?php foreach ($subchild as $subchildcategory) : ?>
<?php $path=$subchildcategory->getRequestPath()?>
<?php break;?>
<?php endforeach ?>
<a href="<?php echo $this->getUrl().$path; ?>">
<?php echo $name= $subcategory->getName().$subcategory->getEnable() ?>
</a>
</li>
<?php endif;?>
<?php endforeach; ?>
</ul>

You can try :
children = Mage::getModel('catalog/category')->getCategories($defaultcategory)
->addAttributeToSort('name', 'ASC');
or :
children = Mage::getModel('catalog/category')->getCategories($defaultcategory)
->setOrder('name','ASC);

$categories = Mage::helper('catalog/category');
$collection = $categories->getStoreCategories(false,true,false);
foreach($collection as $_category)
{
//Do something
echo $_category->getName();
}
use this code to get collection and follow this link for more detailIn magento - same code for getting all categories running well with sorted in localhost but not in web server

Related

Display all the Post Categories in Magento Fishpig

I've two Post Categories with two different layouts, But now both are displaying in the same view.phtml. I need to create a check in which category the post belongs and display the style accordingly.
By using below method, I can load a single category with ID 2.
<?php $test = Mage::getModel('wordpress/term')->load(2);?>
Is there any way to load all the post categories.?
Shyam is almost there. Here is a slightly cleaner version of the code:
<?php $categories = $post->getTermCollection('category') ?>
<?php if (count($categories) > 0): ?>
<?php foreach($categories as $category): ?>
<?php if ((int)$category->getId() === 1): ?>
// Category ID #1
<?php elseif ((int)$category->getId() === 2): ?>
// Category ID #2
<?php else: ?>
// All other categories
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
By this method, you can split posts according to category and display in the same view.phtml with different layouts, for adding different layouts paste your code inside the if($getCategory == cat_id) section as I mentioned below.
<?php $categories = $post->getTermCollection('category') ?>
<?php if (count($categories) > 0): ?>
<?php foreach($categories as $category): ?>
<?php
$getCategory = $this->escapeHtml($category->getId());
echo "Get cat: ".$getCategory;
if($getCategory == 2)
{
//your code here
}
if($getCategory == 3)
{
//your code here
}
<?php endforeach; ?>
<?php endif; ?>

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>

Displaying posts from specific categories with the AheadWorks blog extension for Magento

I'm using the AheadWorks blog extension for Magento, and my blog pages are working fine. But I want excerpts of my latest posts from certain categories to appear on my home page. I've successfully setup everything thus far by adding:
<block type="blog/blog" name="blog.latest" template="aw_blog/blog-home.phtml" />
to "layout.xml", by adding:
<?php echo $this->getChildHtml('blog.latest') ?>
to my home page's phtml file, and by creating "template/aw_blog/blog-home.phtml".
The problem is that I can't figure out how to limit what categories are shown. For example, you'll see in my "blog-home.phtml" file below that I'm trying to limit the posts to the "news" category. I've tried lots of solutions from other forums, but no matter what I do, I see posts from every category. Does anyone know what I need to add/take away from my code to limit the categories?
<?php $posts = $this->getPosts("news"); ?>
<div id="messages_product_view">
<?php Mage::app()->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton('customer/session')->getMessages(true)); ?>
<?php echo Mage::app()->getLayout()->getMessagesBlock()->getGroupedHtml(); ?>
</div>
<?php $numberOfPosts = 1 ?>
<?php $renderedPosts = 0 ?>
<?php foreach ($posts as $post): ?>
<div class="postWrapper">
<div class="postTitle">
<h2><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h2>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
<?php echo $this->getBookmarkHtml($post) ?>
<div class="tags"><?php echo $this->getTagsHtml($post) ?></div>
<div class="postDetails">
<?php if ($this->getCommentsEnabled()): ?>
<?php echo $post->getCommentCount(); ?> <a href="<?php echo $post->getAddress(); ?>#commentBox" >Comments</a> |
<?php endif; ?>
<?php $postCats = $post->getCats(); ?>
<?php echo "<h1>" . $postCats[2] . "</h1>"; ?>
<?php if (!empty($postCats)): ?>
<?php echo Mage::helper('blog')->__('Posted in'); ?>
<?php foreach ($postCats as $data): ?>
<?php echo $data['title']; ?>
<?php endforeach; ?>
<?php else: ?>
<?php endif; ?></div>
<?php $renderedPosts ++ ?>
<?php if ($renderedPosts = $numberOfPosts) {
break;
}
?>
</div>
<?php endforeach; ?>
<?php //$this->getPages(); ?>
Following is the quick solution:
Go to aw_blog frontend template, default might be this app/design/frontend/base/default/template/aw_blog/menu.phtml
check and replace:
if ($posts = $this->getRecent():
with
$currentBlogCat = Mage::getSingleton('blog/cat');
if ($posts = $this->getRecent($currentBlogCat['cat_id'])):
Now open up /app/code/community/AW/Blog/Block/Menu/Sidebar.php
check for the below function:
public function getRecent()
add a parameters to it:
public function getRecent($currentCat=false)
Also, replace the following code block
$collection = clone self::$_collection;
if (array_key_exists('categories',$data = $this->getData();) && count(explode(',', $data['categories'])) == 1) {
with
$collection = clone self::$_collection;
$data = $this->getData();
if($currentCat>0)$data['categories']=$currentCat;
# Category fix #
if (array_key_exists('categories',$data ) && count(explode(',', $data['categories'])) == 1) {
Thanks!

what's those line meaning in magento?

<?php foreach ($this->getChildGroup('detailed_info', 'getChildHtml') as $alias => $html):?>
<div class="box-collateral <?php echo "box-{$alias}"?>">
<?php if ($title = $this->getChildData($alias, 'title')):?>
<h2><span><?php echo $this->escapeHtml($title); ?></span></h2>
<?php endif;?>
<?php echo $html; ?>
</div>
<?php endforeach;?>
<?php echo $this->getChildHtml('product_additional_data') ?>
what's those line meaning in magento?
foreach over each child (that is grouped by name detailed_info with method getchildhtml) and output the data from those blocks
get the html of product_additional_data block
This is the snippet of code from the catalog/product/view.phtml that displays Description, Custom Options, and other detailed product information in the front end view.

Magento getProductCount() on subcategory

the getProductCount() in the second level of category echo count print out 0, i try different way but i diddn't figure out like mage get collection etc, i didn'd find any solution at this question
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<?php foreach ($collection as $cat):?>
<?php
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$_img = $cur_category->getThumbnailUrl();
?>
<div class="grid_4">
<div class="mineContent_grid_4">
<dl>
<dt>
<a href="<?php echo $helper->getCategoryUrl($cat);?>">
<?php echo $cat->getName();?>
<img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>" width="173" height="208"/>
</a>
</dt>
<?php $childLevel2Category = Mage::getModel('catalog/category')->getCategories($cat->entity_id);
?>
<dd>
<ol>
<?php foreach ($childLevel2Category as $catLevel2) { ?>
<?php
$cur_category2 = Mage::getModel('catalog/category')->load($cat->getId());
$count = $cur_category2->getProductCount();
?>
<li> <?php echo $catLevel2->getName();?> <span>(<?php echo $count ?>)</span></li>
<?php } ?>
</ol>
</dd>
</dl>
</div>
</div>
<?php endforeach;?>
You have this code:
<?php
$cur_category2 = Mage::getModel('catalog/category')->load($cat->getId());
$count = $cur_category2->getProductCount();
?>
This loads $cur_category2 with $cat->getId(), which is your parent category and not the current category. I think you want this:
<?php
$cur_category2 = Mage::getModel('catalog/category')->load($catLevel2->getId());
$count = $cur_category2->getProductCount();
?>
Here's a snippet that should help you. All my snippet does is get the product count for the category. I hard coded a category ID of 4, but your code should work, getting the current category. You may want to isolate this code into a function to keep it simpler, then just reference it from your existing page. It essentially loads a collection of products, by category, filtering out products that aren't visible.
$_category = Mage::getModel('catalog/category')->load(4);
$collection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($_category);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
echo $collection->count();
try this example
$categories = Mage::getModel('catalog/category')->load(2)->getChildrenCategories();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$productCollection->addCountToCategories($categories);
var_dump($categories);
where 2 - category id
also check php class Mage_Catalog_Block_Navigation in Magento

Resources