getChildrenCategories not working in magento, sort category by position in admin - magento

i won't to sort categories in frontend as in admin if i use getChildren then it will not sort categories as in admin but if i use getChildrenCategories then no result
<?php
$cats = Mage::getModel('catalog/category')->load(74)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $data): ?>
<li>
<a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>">
<img class="cat-image" src="<?php echo $data['img']; ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>

Use an ordered collection. See below example:
<?php
$category = Mage::getModel('catalog/category')->load(74);
$collection = $category->getCollection();
->addAttributeToSelect('name')
->addAttributeToSelect('image')
->addIdFilter($category->getChildren())
->setOrder('name', Varien_Db_Select::SQL_DESC);
You can then loop through the collection to render the items.

Related

How to display 3 levels of hierarchical menu in WordPress using "wp_get_nav_menu_items"?

I am trying to display 3 levels of hierarchical menu in WordPress
using wp_get_nav_menu_items function, but somehow the structure is not
displaying in that manner.
This is my code which I have included in header file, but the structure is not proper.
$menu_name = 'primary-menu';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC'));
$ParentArray = array();
foreach ($menuitems as $item) {
// if (!empty($item->menu_item_parent) && !in_array($item->menu_item_parent, $ParentArray)) {
array_push($ParentArray, $item->ID);
// }
}
?>
<nav>
<ul class="main-nav">
<?php
$count = 0;
$submenu = false;
foreach ($menuitems as $item):
$link = $item->url;
$title = $item->title;
// item does not have a parent so menu_item_parent equals 0 (false)
if (!$item->menu_item_parent):
// save this id for later comparison with sub-menu items
$parent_id = $item->ID;
?>
<li class="item">
<a href="
<?php echo $link; ?>" class="title">
<?php echo $title; ?>
</a>
<?php endif; ?>
<?php if (in_array($item->menu_item_parent, $ParentArray)): ?>
<?php if (!$submenu): $submenu = true; ?>
<ul class="sub-menu">
<?php endif; ?>
<li class="item">
<a href="
<?php echo $link; ?>" class="title">
<?php echo $title; ?>
</a>
</li>
<?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id && $submenu): ?>
</ul>
<?php
$submenu = false;
endif;
?>
<?php endif; ?>
<?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id): ?>
</li>
<?php
$submenu = false;
?>
<?php
$count++;
endforeach;
?>
</ul>
</nav>
After making changes in above code,this things work for me.
$menu_name = 'primary-menu';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC'));
$ParentArray = array();
foreach ($menuitems as $item) {
// if (!empty($item->menu_item_parent) && !in_array($item->menu_item_parent, $ParentArray)) {
array_push($ParentArray, $item->ID);
// }
}?>
<nav>
<ul class="main-nav">
<?php
$count = 0;
$submenu = false;
foreach ($menuitems as $item):
$link = $item->url;
$title = $item->title;
// item does not have a parent so menu_item_parent equals 0 (false)
if (!$item->menu_item_parent):
// save this id for later comparison with sub-menu items
$parent_id = $item->ID; ?>
<li class="item">
<a href="
<?php echo $link; ?>" class="title">
<?php echo $title; ?>
</a>
<?php endif; ?>
<?php if (in_array($item->menu_item_parent, $ParentArray)): ?>
<?php if (!$submenu): $submenu = true; ?>
<ul class="sub-menu">
<?php endif; ?>
<li class="item">
<a href="
<?php echo $link; ?>" class="title">
<?php echo $title; ?>
</a>
</li>
<?php if (!isset($item[$count + 1]) || $item[$count + 1]->menu_item_parent != $parent_id && $submenu): ?>
</ul>
<?php
$submenu = false;
endif;
?>
<?php endif; ?>
<?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id): ?>
</li>
<?php
$submenu = false;
?>
<?php
$count++;
endforeach;
?>
</ul>
</nav>

LIst Magento Products in a block and split them into columns

I am searching for a way to list magento products from a certain category in a block. I created a phtml for extracting the products:
<?php
$categoryid = 4;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<?php echo $_product->getName(); ?><br></li>
<?php } ?>
Then I included a block to this phtml file.
How can I split the products into columns with a break?
Like
1 6 11 ...
2 7 12
3 8 13
4 9 14
5 10 15
Any solution would be great! I think this is just PHP.
Thanks a lot!
Hans
Thanks Mahmood Rehman,
I tried this version, but it did also not work:
<?php
$categoryid = 4;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
?>
<ul>
<?php $i=0;
foreach ($collection as $_product) { ?>
<?php if ($i++%4 == 0): ?>
</ul><ul>
<?php endif ?>
<li>
<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) ?>" />
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
<br></li>
<?php } ?>
</ul>
I think you can do like this :
<ul>
<?php $i=0;
foreach ($collection as $_product) { ?>
<?php if ($i++%4 == 0): ?>
</ul><ul>
<?php endif ?>
<li>
<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) ?>" />
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
<br></li>
<?php $i++;} ?>
</ul>

Display magento products by category ID

I need to know how can I display products in a page like (cart, below total) only few products by ID. Eg: products with id 2,3,4 and 5.
<div class="freeProducts voucher code">
<?php
$categoryid = 64;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /> <?php echo $_product->getName(); ?>
<?php } ?>
</div>
At this moment I can see the image for each product, and the title. I need to display the ADD TO CART and price.
Anyone can help please?
get Product from specific category
$categoryIds = array(2,4);//category id
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => $categoryIds))
get Product for specific product id
$productids = array(52,62);//product ids
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addFieldToFilter('entity_id',array( 'in' => $productids));
then write this in phtml
<?php $_collectionSize = $collection->count() ?>
<?php //$_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($collection as $product): ?>
<?php if ($i++%4==0): ?>
<ul class="products-grid">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>">
<img src="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(197, 167); ?>" alt="<?php echo $product->getName()?>" border="0" />
</a>
<h2 class="product-name"><?php echo $product->getName() ?></h2>
<div class="price-box">
<?php echo Mage::helper('core')->currency($product->getPrice(),true,false);?>
</div>
<div class="actions">
<?php if($product->isSaleable()): ?>
<button class="button" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add/')?>product/<?php echo $product->getId() ?>/')" title="<?php echo $this->__('Köp');?>" type="submit"><span><span><?php echo $this->__('Köp');?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
hope this help you
// print_r($productslist)
$category_id = 14; // if you know static category then enter number
$catagory_model = Mage::getModel('catalog/category')->load($category_id);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($catagory_model); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()'); //uncomment to get products in random order
$collection->addStoreFilter();
if(!empty($collection))
{
foreach ($collection as $_product):?>
<img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(197, 167); ?>" />
<?php endforeach;
}else
{
echo 'No products exists';
}
$categoryid = 123;
$_category = Mage::getModel('catalog/category')->load($categoryid);
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($_category);
<?php
require "../app/Mage.php";
Mage::app();
?>
<table>
<?php
$ris_arr = array(39,77,78,79);
foreach ($ris_arr as &$value) {
?>
<tr><td style="background-color:#foo;margin-top:50px;">
<?php
$ris_value = $value;
$category_id = $ris_value; // if you know static category then enter number
$catagory_model = Mage::getModel('catalog/category')->load($category_id);
$collection = Mage::getResourceModel('catalog/product_collection')->setPageSize(4);;
$collection->addCategoryFilter($catagory_model); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToSelect(array('name','url','small_image','price','sku')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()'); //uncomment to get products in random order
$collection->addStoreFilter();
if(!empty($collection))
{ ?>
<div style="width:100%">
<?php foreach ($collection as $_product):?>
<div style="width:200px;float:left">
<?php $pro_url= $_product->getProductUrl();
$pro_url2=str_replace("rishabh_daily_report_cron2.php/","",$pro_url);?>
<a href="<?php echo $pro_url2;?>">
<img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(197, 167); ?>" /> </a>
<?php
echo $_product->getName()."<br>";
//echo $_product->getSku()."<br>";
echo $_product->getSpecialPrice();
?>
</div>
<?php endforeach; ?>
</div>
<?php
}else
{
echo 'No products exists';
}
echo "</td></tr>";
}
?>
<table>

How to show children's subcategorie's of current categorie in Magento

I am trying to get my big catalog somewhat tablet friendly by offering an selection of tabs with sublevel links for my seperate categories. So if a user clicks a (1st level)head category it needs to display a nr of blocks which hold the picture, description and url of each direct child and a list of all the underlying (3rd level) child categories of the shown (2nd level) categorie. Would any off you guys be so kind to check my code?
<?php
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$_categories = $_category->getCollection()
->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($_category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite();
?>
<?php $children = explode( ",", $this->getCurrentCategory()->getChildren() ); ?>
<ul class="category-grid">
<div class="category-list">
<?php foreach( $children as $child ): ?>
<?php $_child = Mage::getModel( 'catalog/category' )->load( $child ); ?>
<li class="item">
<img title="<?php echo $this->htmlEscape($_child->getName()) ?>" src="<?php echo $this->htmlEscape($_child->getImageUrl()) ?>" alt="<?php echo $this->htmlEscape($_child->getName()) ?>" />
<div class="subcategory-title">
<?php echo $this->htmlEscape($_child->getName()) ?>
</div>
<div class="description-block"> <?php echo $_child->getDescription(); ?></div>
<div class="children-links"><?php
$_helper = Mage::helper("catalog/category");
$rootCat = Mage::app()->getStore()->getRootCategoryId();
$current = Mage::registry('current_category');
if ($child){
//look for anchestor
$parentid = $child->getParentId();
$parent = Mage::getModel("catalog/category")->load($parentid);
if($parentid != $rootCat)
{
//find the anchestor
show_cat($parent,$_helper,$rootCat);
}else{
//is root
$_subcategories = $child->getChildrenCategories();
echo $_child->getAll_Children();
if(count($_subcategories)>0){
echo '<ul>';
foreach($_subcategories as $_category){
echo '<li>';
echo ''.$_category->getName().'';
if($child->getId() == $_category->getId()){
$current = Mage::registry('current_category');
if ($current){
//handle current
$_current_subcategories = $current->getChildrenCategories();
if(count($_current_subcategories)>0){
//the current cat has childrens
echo '<ul>';
foreach($_current_subcategories as $_sub_category){
echo '<li>';
echo ''.$_sub_category->getName().'';
echo '</li>';
}
echo '</ul>';
}else{
//the current cat has no childrens
$current_parent = $current->getParentId();
$current_parent = Mage::getModel("catalog/category")->load($current_parent );
$_current_subcategories = $current_parent ->getChildrenCategories();
echo '<ul>';
foreach($_current_subcategories as $_sub_category){
echo '<li>';
echo ''.$_sub_category->getName().'';
echo '</li>';
}
echo '</ul>';
}
}
}
echo '</li>';
}
echo '</ul>';
}
}
}
?>
</div>
</li>
<?php endforeach ?>
</div>
</ul>
you can this by the below code and also refer link at bottom
you could be go with this
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul class="category">
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php if ($currentCategory->getId() && $currentCategory->getId() == $_category->getId()): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul class="subcategory">
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
EDIT
<ul class="subcategory">
<? foreach ($_categories as $_category):?>
<? if($_category->getIsActive()):
$cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_subcategory);
?>
<li> <?php echo $_category->getName()?></li>
<? endif;?>
<?endforeach?>
</ul>
Or you can go throw this Detail documentation, i am sure that would be really helpful to you.
// get current category
$current_category = $layer->getCurrentCategory();
// get sub categories of current category
$parent_categories = Mage::getModel('catalog/category')->getCategories($current_category->getId());
// go through each sub category and get their sub categories.
foreach($parent_categories as $child_category)
{
$child_category_id = $child_category->getId();
$grandchild_categories = Mage::getModel('catalog/category')->getCategories($child_category_id);
}
Here is the tested code for showing sub categories. Just put this code above "Toolbar" code in your custom theme "Magento_catalog/templet/product/list.phtml"
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
$subcats = $category->getChildrenCategories();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$imageHelper = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Helper\Image::class);
if(count ($subcats) > 0)
{
?>
<div class = "sub-cat-div">
<ul class = "sub-cat-main">
<?php
foreach ($subcats as $subcat) {
if ($subcat->getIsActive()) {
$_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
$_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
$subcaturl = $_category->getUrl();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<img src="' . $_imgUrl . '" />';
$_imgHtml = $_outputhelper->categoryAttribute($_category, $_imgHtml, 'image');
echo '<li class="sub-cat-image">' . $_imgHtml . '<span style="background-color: rgba(255,255,255,0.9)" class="content bg-white"><strong>' . $_category->getName() . '</strong></span></li>';
}
else{
$_imgUrl = $imageHelper->getDefaultPlaceholderUrl('image');
$_imgHtml = '<img src="' . $_imgUrl . '" />';
$_imgHtml = $_outputhelper->categoryAttribute($_category, $_imgHtml, 'image');
echo '<li class="sub-cat-image">' . $_imgHtml . '<span style="background-color: rgba(255,255,255,0.9)" class="content bg-white"><strong>' . $_category->getName() . '</strong></span></li>';
}
}
} ?>
</ul>
</div>
<?php
}
?>

How to Display the categories in tree structure

How to display all the categories in tree structure alike in a pop up window ie.) if i click the select category button in my page it should show the popup window with the tree
structured categories.i tried like this which will show all the categories in dropdown that does not look good
<?php
$categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addIsActiveFilter();
$allcatid = array();
$k=0;
foreach ($categories as $c) {
$allcatid[$k] = $c->getId();
$k++;
}
$finalcat=array_shift($allcatid);
$root= Mage::app()->getStore()->getRootCategoryId();
?>
<select id="category" class="myinput-text required-entry widthinput" name="category" >
<?php foreach($allcatid as $keycat){?>
<option value="<?php echo $keycat;?>"><?php echo Mage::getModel("catalog/category")->load($keycat)->getName(); ?></option>
<?php } ?>
This will help you to fetch you category tree
<?php
$rootcatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
function get_categories($categories) {
$array= '<ul>';
foreach($categories as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$count = $cat->getProductCount();
$array .= '<li>'.''. $category->getName() . "(".$count.")\n";
if($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$array .= get_categories($children);
}
$array .= '</li>';
}
return $array . '</ul>';
}
echo get_categories($categories); ?>
Output View
This code is included an end-level category with product count.
Step 1 Goto theme/layout/catalog.xml put this code in the file.
<reference name="left">
<block type="catalog/navigation" name="category_list_sidebar" template="catalog/navigation/categorymenu.phtml"/>
</reference>
Step 2 Goto theme/template/catalog/ and create navigation folder also create categorymenu.phtml file inside folder and put code into file.
<?php
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
$currentCategory = Mage::registry('current_category');
?>
<div class="block block-list block-categorys">
<div class="block-title">
<strong><span>Category</span></strong>
</div>
<div class="block-content">
<ul class="category_sub">
<?php
if (count($_categories) > 0){
global $index;
global $data;
foreach($_categories as $_category){
$check_child_class = check_child_par($_category->getId());
$collaps = ($check_child_class)? "<span class='show-cat'>+</span>" : "";
echo "<li class='".$check_child_class."'>";
echo "<a href='".$_helper->getCategoryUrl($_category)."'>".$_category->getName();
echo " (".product_count($_category->getId()).")";
echo "</a>".$collaps;
echo check_child($_category->getId());
echo "</li>";
}
}
?>
</ul>
</div>
</div>
<?php
function check_child($cid){
$_helper = Mage::helper('catalog/category');
$_subcategory = Mage::getModel('catalog/category')->load($cid);
$_subsubcategories = $_subcategory->getChildrenCategories();
if (count($_subsubcategories) > 0){
echo "<ul>";
foreach($_subsubcategories as $_subcate){
$check_child_class = check_child_par($_subcate->getId());
$collaps = ($check_child_class)? "<span class='show-cat'>+</span>" : "";
echo "<li class='".$check_child_class."'>";
echo "<a href='".$_helper->getCategoryUrl($_subcate)."'>".$_subcate->getName();
echo " (".product_count($_subcate->getId()).")";
echo "</a>".$collaps;
echo check_child($_subcate->getId());
echo "</li>";
}
echo "</ul>";
}else{
return "";
}
}
function check_child_par($cid){
$_subcat = Mage::getModel('catalog/category')->load($cid);
$_subsubcats = $_subcat->getChildrenCategories();
if (count($_subsubcats) > 0){
return "parent";
}else{
return "";
}
}
function product_count($cid){
$products_count = Mage::getModel('catalog/category')->load($cid)->getProductCount();
return $products_count;
}
?>
enter code here
<?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; ?>
this will fetch all catgory and sub category,,,hope this will help you

Resources