How to add pagination in subcategory collection in magento? - php

I have written following code to display subcategories
<ul class="">
<?php
$_categories=$this->getCurrentChildCategories();
if($_categories->count()):
$categorycount = 0;
foreach ($_categories as $_category):
if($_category->getIsActive()):
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$catName = $this->getCurrentCategory()->getName();
?>
<li class="">
<div class="">
<div class="">
<div class=""> <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="">
<?php
$imageUrl = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/".$this->getCurrentCategory()->getThumbnail();
$imageResized = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/"."resize/".$this->getCurrentCategory()->getThumbnail();
if (!file_exists($imageResized) && file_exists($imageUrl))
{
$imageObj = new Varien_Image($imageUrl);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(TRUE);
$imageObj->keepFrame(FALSE);
$imageObj->quality(100);
$imageObj->resize(270, 270);
$imageObj->save($imageResized);
}
?>
<span class=""><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/resize/'.$this->getCurrentCategory()->getThumbnail(); ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>"/></span> </a> </div>
</div>
<div class="">
<div class="">
<div class=""> <?php echo $this->htmlEscape($_category->getName()) ?> </div>
</div>
</div>
</div>
</li>
<?php
endif;
endforeach;
endif;
?>
</ul>
I want to add pagination in this page, i have tried solutions like following but it does not worked either
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
$pager->setAvailableLimit(array(15=>15));
$pager->setCollection($_categories);
$this->setChild('pager', $pager);
Can anyone have any idea on how to add pagination in subcategory listing page?
Please guide me as i am new to magento development.
Thanks in advance.

Inside Mage_Catalog_Block_Navigation I have custom method :
public function getCurrentChildCategories()
{
if (null === $this->_currentChildCategories) {
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$this->_currentChildCategories = Mage::getModel('catalog/category')->getCollection();
$pager = new Mage_Page_Block_Html_Pager();
$pager->setLimit(100)->setCollection($this->_currentChildCategories);
$this->setChild('pager', $pager);
/* #var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$this->_currentChildCategories->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('is_anchor')
->addAttributeToSelect('image')
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('name', 'ASC')
->joinUrlRewrite()
->load();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($this->_currentChildCategories);
}
return $this->_currentChildCategories;
}
I think it's not the best but it works.

Related

Magento 'Featured Categories' Images Not Loading on Frontend

In my Magento (1.9.3.3) Admin panel, I have images uploaded for each category, and I'm able to see them from the category edit form. However, on the front-end homepage, my featured images are not showing. If I inspect the home page, each category image src is blank:
<img alt="" src="">
The code that generates this is as follows:
<?php
$_subcategories = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('is_featured', 1)
->addAttributeToSort( 'feature_position');
$_subcategories->getSelect()->limit(7,0);
$categories_count = count($_subcategories);
?>
<div class="row">
<?php
$count = 0;
if($_subcategories): ?>
<ul id="myList">
<?php foreach ($_subcategories as $cat):
$count = $count + 1;
if( $count > 6 ) {
break;
}
?>
<li class="col-md-4 cat-item col-sm-4 col-xs-12" style="display: list-item;">
<a class="image-link" href="<?php echo $cat->getUrl($cat);?>"><div class="feature-cat"><img alt="" src="<?php echo $cat->getImageUrl();?>">
<div class="feature-cat-name"><?php echo $cat->getname(); ?></div>
</div>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
So, the final HTML output for each category looks like this:
<a class="image-link" href="http://127.0.0.1/product-category/product1">
<div class="feature-cat"><img alt="" src="">
<div class="feature-cat-name">Batteries</div>
</div>
</a>
Any ideas what $cat->getImageUrl() is coming back NULL, but all the other category attributes are fine? I've tried a number of things with no success. Thank you!
Also add below attribute to filters and then try.
$_subcategories = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('is_featured', 1)
->addAttributeToSort( 'feature_position')
->addAttributeToSelect('image');
Then get the image for each category in for loop.
foreach($_subcategories as $subcat) {
echo "Image : ".$subcat->getImage();
}
Try this
if($_category->getImageUrl()){
$catimg = $_category->getImageUrl();
}else{
$catimg=null;
continue;
Or just
$catimg = $_category->getImageUrl();
This worked:
$_subcategories =
Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('is_featured', 1)
->addAttributeToSort( 'feature_position')
->addAttributeToFilter('image', array('notnull' => true));

load product in particular category in magento

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..

get category url through product url

how i can get category url through product url
i have product url but i want to get category url
i want to redirect into category page when user click on product name
Site Link
my code is :
<?php
if($this->getItems()->getSize()):
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
?>
<div class="col-lg-7 col-md-7 col-sm-7 cpl-xs-12">
<div class="ndlSimilarTop">
<div class="ndlSimilatProductTitle">RECOMMENDED PRODUCTS</div>
<div id="amazingcarousel-container-1" style="overflow:hidden">
<div id="amazingcarousel-1" style="display:block;position:relative;width:100%;max-width:711px;margin:0px auto 0px;">
<div class="amazingcarousel-list-container" style="overflow:hidden;">
<ul class="amazingcarousel-list">
<?php foreach($this->getItems() as $_item): ?>
<li class="amazingcarousel-item">
<div class="amazingcarousel-item-container">
<div class="amazingcarousel-image">
<img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(217, 173) ?>" alt="product-img" />
<ul class="mask mask1">
<li class="ndlHoverContent ndlHoverContent1">
<img src="<?php echo $this->getSkinUrl() ?>images/wishlist-white-icon.png" alt="">Add to wishlist
<img src="<?php echo $this->getSkinUrl() ?>images/mybag-white-icon.png" alt="">Add to bag
</li>
</ul>
</div>
<?php echo "Product Url:".$_item->getProductUrl();
echo "CAT Url:".$_item->getCategoryUrl();
?>
<div class="ndlListDetail">
<div class="ndlProductListName"><?php echo $this->htmlEscape($_item->getName()) ?>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
</div>
</div>
</li>
<?php endforeach; ?>
One such solution can be below:
<?php
$productUrlPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html';
$rewrite = Mage::getModel('core/url_rewrite')
->setStoreId(Mage::app()->getStore()->getId())
->loadByRequestPath($productUrlPath);
$productId = $rewrite->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
$categoriesId = $product->getCategoryIds();
foreach($categoriesId as $categoryId){
$category = Mage::getModel('catalog/category')->load($categoryId);
echo $category->getUrl();
}
?>
Note: You can have more than one category of product.

PHP/Magento: Show, sort , and limit by products based on subcategory on category page

How do I sort products on a category page by subcategory as well as limit the number of products from each subcategory:
For example if the category was Food I would want to display the following:
Drinks Coke 12oz, Orange Juice 8oz, Milk Gallon,
Pasta, Spaghetti 1lb, Pesto 12 pc, Tortellini 1 PC.
And so on, displaying each subcategory name followed 3 products (images etc.)
I currently have a custom template that displays the subcategories but can't figure out the products,
<?php
$_category = $this->getCurrentCategory();
$collection = $_category->getCollection()
->addAttributeToSelect(
array('url_key','name','all_children','is_anchor','description','image')
)
->addAttributeToFilter('is_active', 1)
->addIdFilter($_category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite();
$helper = Mage::helper('catalog/category');
?>
<ul>
<?php foreach ($collection as $cat): ?>
<li>
<div class="level1descript">
<a href="<?php echo $helper->getCategoryUrl($cat); ?>">
<img src="<?php echo $cat->getImageUrl(); ?>" class="catlevel1image" />
<h2><?php echo $cat->getName(); ?></h2>
</a>
<p class="level1descript">
<?php
$catdesc = '';
$catdesc = strip_tags($cat->getDescription());
if (strlen($catdesc) > 300) {
$catdesc = substr($catdesc, 0, 300) . ' ...';
}
echo $catdesc;
?>
</p>
</div>
<?php
$childLevel2Category = $cat->getCollection()
->addAttributeToSelect(
array('url_key','name','all_children','is_anchor','description','image')
)
->addAttributeToFilter('is_active', 1)
->addIdFilter($cat->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite();
?>
<ul>
<?php foreach ($childLevel2Category as $catLevel2) { ?>
<li class="level2cats">
<a href="<?php echo $helper->getCategoryUrl($catLevel2); ?>">
<img src="<?php echo $catLevel2->getImageUrl(); ?>" class="catlevel2image" />
<h4><?php echo $catLevel2->getName(); ?></h4>
</a>
<p class="level2descript">
<?php
$catdesc = '';
$catdesc = strip_tags($catLevel2->getDescription());
if (strlen($catdesc) > 60) {
$catdesc = substr($catdesc, 0, 60) . ' ...';
}
echo $catdesc;
?>
</li>
<?php } ?>
</ul>
</li>
<?php endforeach;?>
</ul>
Below I documented an Idea, I think this is an idea.. Please excuse my rough throw together style wise. As I just used a project I was working on to throw this together. Any questions please ask. The setPageSize method will pull the first 3 products that display by default in the subcategories.
<!-- Finding Current Category and Finding it's children -->
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<div class="subcategories">
<p>Select a category to view products:</p>
<ul class="clearfix">
<!-- Display Each Subcategory Image and Name -->
<?php foreach ($categories as $category): ?>
<li class="grid12-3">
<a href="<?php echo $category->getUrl() ?>" class="clearfix">
<?php if($thumbFile = $category->getThumbnail()): ?>
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<?php endif;?>
<span><?php echo $category->getName() ?></span></a>
</li>
<!-- Load (3) Products from within each subcategory -->
<?php
$_helper = $this->helper('catalog/output');
$products = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->setPageSize(3)
->addAttributeToSelect(array('name', 'product_url', 'small_image'))
->load();
?>
<!-- Display Each product's detailed info -->
<?php foreach ($products as $product): ?>
<li>
<?php // Product Image ?>
<img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />
<?php // Product description ?>
<?php $_productNameStripped = $this->stripTags($product->getName(), null, true); ?>
<h2 class="product-name"><?php echo $_helper->productAttribute($product, $product->getName() , 'name'); ?></h2>
</li>
<?php endforeach ; ?>
<?php endforeach; ?>
</ul>
</div>

Get Magento Categories with Especific Sort of Admin Panel

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();

Categories