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));
Related
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.
This is my footer. There is parade of categories with most articles in. I need to exclude here categories with description that starts with "XXX".
So If some categories have description that starts with "XXX", it may donĀ“t show here.
Is it possible please? Im newbie in PHP so I dont know if can I declare category discreption here.
<?php global $teo_options;?>
<footer role="contentinfo">
<?php if(isset($teo_options['enable_popular_companies']) && $teo_options['enable_popular_companies'] == 1) { ?>
<div class="stripe-regular">
<div class="row">
<div class="column">
<h2><?php _e('Name', 'Couponize');?></h2>
</div>
</div>
<div class="row collapse">
<div class="column">
<div class="popular-companies flexslider">
<ul class="rr slides">
<?php
$args['hide_empty'] = 1;
$args['orderby'] = 'count';
$args['order'] = 'desc';
if(isset($teo_options['blog_category']) && $teo_options['blog_category'] != '')
$args['exclude'] = implode(",", $teo_options['blog_category']);
$categories = get_categories($args);
foreach($categories as $category) {
$image = get_option('taxonomy_' . $category->cat_ID);
$image = $image['custom_term_meta'];
?>
<li>
<a href="<?php echo get_category_link( $category->term_id );?>" class="wrapper-5 small">
<img src="<?php echo aq_resize($image, 130, 130, true); ?>" alt="<?php echo $category->name;?> coupons">
</a>
</li>
<?php } ?>
</ul>
</div>
</div>
</div>
</div>
<?php } ?>
Everything is possible but you're just complicating your problem.
Why would you identify a category by a piece of text in the Description?
Also, searching in the description as it's text could end to be a slow and unnecessary query, if you have a lot of categories.
To solve it, I recommend you take a look at the documentation about Including & Excluding Categories.
What I would do is to make sub-categories and either hide them manually or do a trick between the child and parent categories.
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.
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>
In my Customization , i have more than 100 categories under Default Category so I want to Display all the categories in multiple pages with images Homepage - Magento.
Im Using the below code :
<div class="category-products">
<ul class="products-grid">
<?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();
if ($categorycount == 0){
$class = "first";
}
elseif ($categorycount == 3){
$class = "last";
}
else{
$class = "";
}
?>
<li class="item <?=$class?>">
<img src="<?php echo $_category->getImageUrl() ?>" width="100" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
<h2><?php echo $this->htmlEscape($_category->getName()) ?></h2>
</li>
<?php
endif;
if($categorycount == 3){
$categorycount = 0;
echo "</ul>\n\n<ul class=\"products-grid\">";
}
else{
$categorycount++;
}
endforeach;
endif;
?>
</ul>
</div>
In this code im just getting only product with images but not in proper manner (not aligned)
please anyone query me how to Display all the categories in multiple pages with images Homepage .
To display in Multiple page you will need to use some pagination techniques ,
to display the product categories do refer
How to create Categories Grid (list) with images