How do I display "Only X left Threshold" on my Magento product grid? - magento

I’m using a custom template I created, and I’m trying to display “Only X left Threshold” set at 1 on the category list(grid) page.
Magento ver. 1.4.1 -
Can anyone help me out?
EDIT
Thanks Alan, solved.
To those who need this for configurable products, here is the code for 'Less than 4 products remaining"
<?php if($_product->getTypeId() == "configurable"):
$total = 0;
$ids = $_product->getTypeInstance()->getUsedProductIds(); ?>
<ul>
<?php foreach ($ids as $id) :
$simpleproduct = Mage::getModel('catalog/product')->load($id); ?>
<li><?php $simpleproduct->getName()." - ".(int)Mage::getModel('cataloginventory/stock_item')
->loadByProduct($simpleproduct)->getQty(); ?>
</li>
<?php $total = $total + (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty(); ?>
<?php endforeach; ?>
<li><?php if ($total < 4) {
echo ("Only ".$total." Left!");
}?></li>
</ul>
<?php endif; ?>

if($product->getQty() < $some_num)
{
echo 'Only' . $product->getQty() . ' left';
}

$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
if($qtyStock < 10) {
echo 'Only' . $qtyStock . ' left';
}

Related

Subcategories in layered navigation filter

I have a category with simple two-level structure like this:
Category #1
- Subcategory
- Subcategory
- ...
Category #2
- Subcategory
- Subcategory
- ...
Currently to filter by subcategories - you have to select top-level category first.
How to show subcategories of all top-level categories in layered navigation filter?
Note: Subcategories should by effected by other selected attribute filter.
While experimenting with Magento files I've found the answer to my question.
Copy app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php to app/code/local/Mage/Catalog/Model/Layer/Filter/Category.php
Open copied file. And replace _getItemsData with code below:
/**
* Get data array for building category filter items
*
* #return array
*/
protected function _getItemsData()
{
$key = $this->getLayer()->getStateKey().'_SUBCATEGORIES';
$data = $this->getLayer()->getAggregator()->getCacheData($key);
if ($data === null) {
// Get root category
$root_category = Mage::getModel('catalog/category')->load(2);
// Get main categories
$data = array();
$main_categories = $root_category->getChildrenCategories();
foreach ($main_categories as $main_category) {
if (!$main_category->getIsActive()) continue; // Ommit inactive
// Get sub categories to list
$sub_categories = $main_category->getChildrenCategories();
// Add count to subcategories
$this->getLayer()->getProductCollection()
->addCountToCategories($sub_categories);
foreach ($sub_categories as $sub_category) {
// Ommit inactive and zero product count sub categories
if ($sub_category->getIsActive() || !$sub_category->getProductCount()) continue;
// Output subcategories
$data[] = array(
'label' => Mage::helper('core')->htmlEscape($sub_category->getName()),
'value' => $sub_category->getId(),
'count' => $sub_category->getProductCount(),
'parent' => $main_category->getName(), // Store parent name to group in template
);
}
}
$tags = $this->getLayer()->getStateTags();
$this->getLayer()->getAggregator()->saveCacheData($data, $key, $tags);
}
return $data;
}
You might be interested in rewriting some other functions such as getResetValue, etc.
I had to rewrite template to group subcategories by main categories.
Result (sorry cant post images directly):
Before:
http://i.stack.imgur.com/skZpi.png
After:
http://i.stack.imgur.com/QxPhq.png
you can use the below code to show all subcategories of a current category in your sidebar or wherever
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Very slow product list issue with magento

I have a category called new arrivals which has 5 subcategories. I created a custom view called new-arrivals.phtml. I created custom Static Block with the following code in magento
{{block type="core/template" template="custom/new-arrivals.phtml"}}
then from Category setting I changed the display setting>CMS Block to the this cms block that I created. And the display mode is static block only
This view bring all the subcategories of New Arrivals(Bags, Tops, Shoes etc..) with it's products.. Like on this website As you can see on this website it is very fast but on my website with my code it is not. Here it is...
How can I make this load faster. Cache and everything is enabled. I assume that there is something wrong with my code. thank you for your help.
and this is new-arrivals.phtml content:
Magento CE 1.8.1
<?php
//I load all the subcategories here
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<?php foreach ($categories as $category): ?>
<div class="page-title category-title">
<h1>
<a href="<?php echo $category->getUrl() ?>">
<span><?php echo $category->getName() ?></span>
</a>
</h1>
<?php
// here I load all the products for each category
$_helper = $this->helper('catalog/output');
$category = Mage::getModel('catalog/category')->load($category->getId());
$_productCollection = $category->getProductCollection()->addCategoryFilter($category)
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('status', 1) // enabled
->addAttributeToFilter('visibility', 4) //visibility in catalog,search
->addAttributeToSort('date_added', 'ASC');
?>
<div class="product-count"><?php echo $count; ?> <?php echo $this->__('Products'); ?></div>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php // Grid Mode ?>
<?php
$_span = 'span3';
$_grid_pro = 4;
?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $_grid_pro; //$this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<?php // all the others stuff ....... ?>
Load this file using ajax or jquery.
Second one you run your code on footer and make one variable then using jquery or java script, you will show your all data in particular div, for example like this
if (count($_subcategories) > 0){
$top_menu .= '<ul>';
foreach($_subcategories as $_subcategory){
$prodCollection = Mage::getModel('catalog/category')- >load($_subcategory->getId())
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id', array('eq' => 'grouped'))
->addAttributeToFilter('no_to_show', array('F' => 78));
if($prodCollection->count() !=0){
$top_menu .= '<li'; if($current_category == $_subcategory->getId()){$top_menu .= ' id="activesub"';}$top_menu .= '>'.$_subcategory->getName().'';
$_subcategories = Mage::getModel('catalog/category')->load($_subcategory->getId());
$_childsub = $_subcategories->getChildrenCategories();
$CatTotalCount = count($_childsub);
if($cntr == 0){
$firstCount = $CatTotalCount;
}
if($firstCount >= 7){
$loopCount = ($firstCount-1);
}else{
$loopCount = 6;
}
if($CatTotalCount >0){
$top_menu .= '<ul>';
$secCntr = 0;
foreach($_childsub as $_child_subcat){
$prodCollection2 = Mage::getModel('catalog/category')->load($_child_subcat->getId())->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('type_id', array('eq' => 'grouped'))->addAttributeToFilter('no_to_show', array('F' => 78));
if( $secCntr>$loopCount){
$secCntr = 0;
$top_menu .= '</ul><ul>';
}
$secCntr++;
if($prodCollection2->count() !=0):
$top_menu .= '<li'; if($current_category == $_child_subcat->getId()){$top_menu .= ' id="activesub"';}$top_menu .= '>'.$_child_subcat->getName().'</li>';
endif;
} }
}
$top_menu .= '</ul></li>';
$cntr++;
}
$top_menu .= '</ul>';
}
jQuery(document).ready(function() {
document.getElementById('top_menus').innerHTML = '';
jQuery('#mega-menu-8').dcMegaMenu({
rowItems: '3',
speed: 'fast',
effect: 'fade'
});
});

Show all subcategories of current parent caregory in sidebar navigation in MAGENTO

Trying to build sidebar category structure in Magento so that all children for an active category show when clicked. Using below as sample, when you go into main catalog only Main Cats appear. Then when clicking any Sub Cat the children for that respective category appear and so on.
For example
Main Cat 1
Sub Cat 1
Sub/Sub 1
Sub/Sub 1
Sub/Sub 1
Sub Cat 1
Sub Cat 1
Main Cat 2
Main Cat 3
Here's the current code I have, but once you get to the last category, only the Main Cats show (in other words, if you click on Sub/Sub, the menu closes and shows only the Main Cats).
<aside id="sidebar">
<div class="sidebar-nav">
<h2><?php echo $this->__('Products') ?></h2>
<ul>
<?php foreach ($store_cats as $cat) {
if ($cat->getName() == $current_cat) {
echo '<li>'.$cat->getName()."<ul>";
foreach ($obj->getCurrentChildCategories() as $subcat) {
echo '<li>'.$subcat->getName()."</li>";
}
echo "</ul></li>";
} else {
echo '<li>'.$cat->getName()."</li>";
}
} ?>
</ul>
</div>
<div class="sidebar-nav">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('holiday-nav-links')->toHtml() ?>
</div>
<div class="sidebar-nav">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('about-us-nav-links')->toHtml() ?>
</div>
</aside>
Any help is much appreciated. Thank you in advance for the help!
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Please check this link http://fishpig.co.uk/magento/tutorials/display-categories-and-subcategories-in-magento/
hope this help you
Had to do this very thing a few days back . get my whole function, may include some unnecessary html parts though. Shows 2nd level categories level onwards (for 3rd level category view as well)
public function getCatTree()
{
$treeHtml = '';
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
$category = Mage::registry('current_category');
$level = $category->getLevel();
switch($level)
{
case 4 :
$level3Cat = $category->getParentCategory();
$level2Cat = $level3Cat->getParentCategory();
break;
case 3 :
$level2Cat = $category->getParentCategory();
break;
case 2 :
$level2Cat = $category;
break;
default :
$level2Cat = null;
break;
}
//get the level 2 category ID
$categoryId = $level2Cat->getId();
if (count($_categories) > 0)
{
foreach ($_categories as $_category)
{
//match with the level 2 category, then list all its children
if ($_category->getId() == $categoryId)
{
$_category = Mage::getModel('catalog/category')->load($_category->getId());
$_catChildrens = $_category->getAllChildren();
foreach(explode(',',$_catChildrens) as $index=>$child)
{
$cat = Mage::getModel('catalog/category')->load($child);
if($cat->getLevel() == 3 && $cat->getIsActive())
{
$isParent = ($cat->hasChildren()) ? 'parent' : '';
$treeHtml .= '<li class="level1 nav'.$cat->getLevel().' '.$isParent.'">'.
'<a href="'.$cat->getUrl().'">'.
$cat->getName().
'</a>';
if($cat->hasChildren())
{
$treeHtml .= '<div class="sub-menu-wrap"><ul class="level1">';
foreach($cat->getChildrenCategories() as $indx=>$_subcategory)
{
if($_subcategory->getIsActive())
{
$cat4 = Mage::getModel('catalog/category')->load($indx);
$treeHtml .= '<li class="level2 nav'.$cat4->getLevel().'">'.
'<a href="'.$cat4->getUrl().'">'.
$cat4->getName().
'</a></li>';
}
}
$treeHtml .= '</ul></div>';
}
$treeHtml .= '</li>';
}
}
return $treeHtml;
}
}
}
}

Get specific category level

How can I get a specific category level from Magento, my category setup looks like this now.
root_catalog
|-Shop
|-Shoes
|-T-shirts
|-Brands
|-Nike
|-Womens
|-Mens
|-Adidas
|-Asics
<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
<?php echo $category->getName(); ?>
<?php endif ?>
When calling $category->getName(); I would like to only display the Brand Name, is that possible?
You can get category level from
$category = Mage::getModel('catalog/category')->load( $categories[1]) )->getLevel()
and then check with your brand name category level, if match then display name.
e.g. suppose brand category level is 3
<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
<?php if($category->getLevel() == 3)
echo $category->getName(); ?>
<?php endif ?>
<?php endif ?>
ANKIT's answer is good, but it could be improved by actually query-ing the specific levels instead of loading the whole collection and doing a conditional. Take for example if you want to get all categories in a specific level:
<ul>
<?php $categories = Mage::getModel('catalog/category')
->getCollection()
// magic is prepared here..
->addAttributeToSelect('*')
// then the magic happens here:
->addAttributeToFilter('level', array('eq'=>2))
->load();
foreach($categories as $category):
?>
<li>$category->getName()</li>
<?php endforeach; ?>
</ul>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$categories = $product->getCategoryIds(); /*will return category ids array*/
foreach($categories as $category){
$cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
if ($cat->getLevel() == 2) {
$catName = $cat->getName().","; ?>
<div class="brand_bg">
<label><?php /* #escapeNotVerified */ echo __('Category :') ?></label>
<?php echo $catName; ?>
</div>
<?php } ?>
<?php } ?>
?>

Make 3rd column in Magento Footer (category list)

I have a list of sub-categories displayed in my footer. Currently the list is in 2 columns, but I would like it to be in 3 columns.
Here is the code I currently have working;
<li class='shop_online'>
<?php
#########################################
$temparray = array();
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
$currentCategory = Mage::registry('current_category');
if (count($_categories) > 0):
foreach($_categories as $_category):
$temparray[] = "<a href='".$_helper->getCategoryUrl($_category)."'>".$_category->getName()."</a>";
/*
$_category = Mage::getModel('catalog/category')->load($_category->getId());
$_subcategories = $_category->getChildrenCategories();
if (count($_subcategories) > 0):
foreach($_subcategories as $_subcategory):
$temparray[] = "<a href='".$_helper->getCategoryUrl($_subcategory)."'>".$_subcategory->getName()."</a>";
$_subsubcategory = Mage::getModel('catalog/category')->load($_subcategory->getId());
$_subsubcategories = $_subsubcategory->getChildrenCategories();
if (count($_subsubcategories) > 0):
foreach($_subsubcategories as $_subsubcategory):
$temparray[] = "<a href='".$_helper->getCategoryUrl($_subsubcategory)."'>".$_subsubcategory->getName()."</a>";
endforeach;
endif;
endforeach;
endif;
*/
endforeach;
endif;
echo "<ul class='footer_links_list'>";
if(count($temparray) > 0)
{
echo '<h3>SHOP ONLINE</h3>';
for( $i=0; $i<count($temparray); $i++ )
{
if(($i+1)%1==0 || ($i+1)%1==0)
echo "<li>".$temparray[$i]."</li>";
}
}
echo "</ul>";
#########################################
?>
</li>
I cannot figure out how to break this into a 3rd column for display in the footer.
formagdev1.com is the dev site I am working on.
Any ideas?
Thanks,
Bill
Just change the width of the below class to 33% (original 50%) in styles.css at line 1808 and you should be good to go.
ul.footer_links_list li
Cheers!

Resources