I am a newbie in Magento and wanted to display all categories on the Magento home page rather than a top nav menu.
I read a lot of articles on it, but nothing has helped. Please point me to the right direction.
Thanks in advance.
The code below does a couple of things, first it will get all the store’s categories - it then checks to see if they are active before continuing.
Code Courtesy : magentocommerce forum
<?php
/* Get the categories that are active for the store */
$_main_categories=$this->getStoreCategories();
/* Get the current category the user is in */
$_current_category=$this->getCurrentCategory();
/* Get the current category path */
$_categorypath = $this->getCurrentCategoryPath();
?>
<ul>
<?php
if ($_main_categories):
/* This bit cycles through the categories - setting the next one to current */
foreach ($_main_categories as $_main_category):
if($_main_category->getIsActive()):
$cur_category=Mage::getModel('catalog/category')->load($_main_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
/* Write the main categories */
?>
<li><?php echo $this->getCurrentCategory()->getName();?></li>
<?php
/* Check the category variable loop against the current category path if it is - print sub categories */
if (in_array($this->getCurrentCategory()->getId(), $_categorypath)): ?>
<?php $_maincategorylisting=$this->getCurrentCategory()?>
<?php $_categories=$this->getCurrentChildCategories()?>
<?php if($_categories->count()):?>
<ul>
<? 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>
<?php /* This resets the category back to the original pages category
**** If this is not done, subsequent calls on the same page will use the last category
**** in the foreach loop
*/ ?>
<?php $layer->setCurrentCategory($_current_category); ?>
<?endif;?>
<?endif;?>
<?php
endif;
endforeach;
else:
?>
<p>$_main_categories array was empty.</p>
<p>This might be because you are referencing this phtml file with a wrong type attribute. You should use <block type="catalog/navigation" ... /> !</p>
<?php endif; ?>
I got where the problem was.
The solution is at this link http://samsami2u.wordpress.com/2009/09/15/add-categories-with-images-on-homepage-magento/ and I was trying to add this line
{{block type="catalog/navigation" name="catalog.category" template="catalog/category/list.phtml"}} to home page from back end in layout update xml under design tab but the correct way was to place it in content tab.
Related
I'm using the code below to get a list of categories on the same level as the current page (by listing the subcategories of this category's parent). It works great when Flat Categories are off. Once I enable Flat Categories, the H2 works correctly, but the list changes to display the root's subcategories instead of this page's parent's subcategories. What am I doing wrong, and why does it act this way?
<?php
$_category = $this->getCurrentCategory();
$parentCategoryId = $_category->getParentId();
$collection = Mage::getModel('catalog/category')->getCategories($parentCategoryId);
$helper = Mage::helper('catalog/category');
$parentCategory = Mage::getModel('catalog/category')->load($parentCategoryId);
?>
<h2><?php echo $parentCategory->getName(); ?></h2>
<ul>
<?php foreach ($collection as $cat):?>
<?php if($_category->getIsActive()): ?>
<li <?php
if ($cat->getId() == $_category->getId()) {
echo 'class="current"';
}
?>>
<?php echo $cat->getName();?>
</li>
<?php endif; ?>
<?php endforeach;?>
</ul>
Yes, I've flushed the cache and indexes.
I don't completely know why ->getCategories($id) is not "flat-safe", but using Stefan's suggestion helped me find an alternate method for this instead.
My new lines at top look like this:
$_category = $this->getCurrentCategory();
$parentCategoryId = $_category->getParentId();
$helper = Mage::helper('catalog/category');
$parentCategory = Mage::getModel('catalog/category')->load($parentCategoryId);
$collection = $parentCategory->getChildrenCategories();
... which works regardless of 'flat catalog' on or off.
I am trying to display category name in product detail page. For that I am using
$cat_name=Mage::registry('current_category')->getName();
It shows category name.
But when I went to wishlist page & click to product image then it give error:-
Fatal error: Call to a member function getName() on a non-object in /opt/lampp/htdocs/dominie/app/design/frontend/default/dominie/template/catalog/product/view.phtml.
Please help me how can I solve this issue.
Just tested the code below and it work in v1.7 when added to template/catalog/product/view.phtml.
However Mage::registry('current_category'); is only available if when coming to a product page from a category page (not tested, but may also dependent on if you have seo url that contain the category names within the url)
<?php
$_helper = $this->helper('catalog/output');
$_category_detail=Mage::registry('current_category');
echo $_category_detail->getName(); //gives current category name
echo $_category_detail->getId(); //gives current category id
?>
See http://vinayp.com.np/how-to-get-category-name-and-id-on-product-page-on-magento/
To display all the categories that a product belong to do
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<?php echo $_category->getName() ?>
<?php endforeach; ?>
See http://www.magentocommerce.com/boards/viewthread/27720/
Try to define $cat_name in your wishlist module so that collection from wishlist module will also contain category name from which it was added to wishlist. This will surely reduce your overhead.
I have a list of store categories, and everything is working well except for one thing. I would like the list of categories to omit any that are set as 'Include in Navivation Menu = No'.
I can tell at this point that this attribute is not being loaded, but I'm having a difficult time figuring out where to place the filter. Currently, I am getting my category list via:
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
Followed by:
<?php foreach ($_categories as $_category) : ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
...
...
At this point, I have my category objects how I want them. But if I run a debug on these objects, the 'include_in_menu' attribute is not listed.
So for some reason:
<?php $_subCategory->getIncludeInMenu() ?>
Was not working for me after I ran:
<?php foreach($_parentCategory->getChildrenCategories() as $_subCategory) : ?>
The object was still the same model, but ['include_in_menu'] was no longer part of the object. I don't like this solution, but I just reconverted the object back:
<?php $_subCategory = Mage::getModel('catalog/category')->load($_subCategory->getId()) ?>
And then it worked fine. Not sure why getting the children would strip down the object, but it does.
In my product view template i'm loading child template, and transfering product instance to be available in this child template:
<?php
echo $this->getLayout()
->createBlock('core/template')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->setAttribute('product', $_product)
->toHtml();
?>
Then in my catalog/product/view/addedToCartDialog.phtml i'm trying to use this product instance:
<?php $product = $this->getData('product'); ?>
<?php echo"<pre>";print_r($product->getId());echo"</pre>"; ?>
However it seems to be not loaded: Fatal error: Call to a member function getId() on a non-object in /home/ryba/workspace/polcode/Greenlights/app/design/frontend/default/greenlights/template/catalog/product/view/addedToCartDialog.phtml on line 2
But when i check variable $product with print_r:
<?php echo"<pre>";print_r($product);echo"</pre>"; ?>
It is displayed that this variable is correct Mage_Catalog_Model_Product Object, also checked if attributes is correct (like sku, name etc) - everything is correct.
What is wrong with this ?
I'm going to give you several answers. The first is the direct answer to your question. The rest are alternatives, but better ways to do what you're trying. The last answer is, in my opinion, the best.
Direct Answer:
Instead of using setAttribute, just use the magic setter/getter methods:
<?php
// In catalog/product/view.phtml
echo $this->getLayout()
->createBlock('core/template')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->setProduct($_product)
->toHtml();
?>
<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>
Better:
And, if you know you are in a template loaded by the catalog/product controller, you can get the product this way.
<?php
// In catalog/product/view.phtml
echo $this->getLayout()
->createBlock('core/template')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->toHtml();
?>
<?php
// In addedToCartDialog.phtml
$_product = Mage::registry('product');
echo $_product->getId();
?>
Even Better
The best way would be to use a different block type which has the methods already loaded (again, if you know you are in a template loaded by the catalog/product controller)
<?php
// In catalog/product/view.phtml
echo $this->getLayout()
->createBlock('catalog/product_view')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->toHtml();
?>
<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>
And Finally, the Best
One last item of business. The better way to add extra blocks to your templates is to add the block in your local.xml file.
<!-- Local.xml -->
<catalog_product_view translate="label">
<reference name="content">
<block type="catalog/product_view" name="addedToCartDialog" as="addedToCartDialog" template="catalog/product/view/addedToCartDialog.phtml" />
</reference>
</catalog_product_view>
Now, set up your phtml file
<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>
Then call the block from your phtml file
// In catalog/product/view.phtml
<?php echo $this->getChildHtml('addedToCartDialog'); ?>
I want to setup the category catalog pages in Magento such that the first page contains the category image and the first three products in that category. Then the following pages contain six products per page without the category image.
I can't figure out how this can be done.
Unfortunately I don't think you can do this without a lot of work. You'll need to rewrite the logic of the pagination, change the page size depending on which page it is, & offset the returned collection.
However you can easily only have the category image displayed on the first page.
This line returns the current page number:
Mage::getBlockSingleton('page/html_pager')->getCurrentPage();
So in template/catalog/category/view.phtml you can just do a conditional around the category image display, find the section:
<?php if($_imgUrl): ?>
<?php echo $_imgHtml ?>
<?php endif; ?>
and replace it with:
<?php if($_imgUrl): ?>
<?php if(Mage::getBlockSingleton('page/html_pager')->getCurrentPage()==1):?>
<?php echo $_imgHtml ?>
<?php endif; ?>
<?php endif; ?>