Fetching current category in magento - magento

Im trying to get the current category from catalog/product/view.phtml
<?php
$_helper = $this->helper('catalog/output');
$item = $this->getProduct();
$curCat = Mage::registry('current_category');
if($curCat && $curCat->getId() == Mage::helper('function')->NEWS_CAT_ID) {
// do stuff
}
?>
The major problem with this method is; Im getting the parrent category_ID(2) instead of the child category_ID(10). How do i fix this problem?

SinisterGlitch, you was goes to this page from catalog search page....registry have save it current category default category ,whose ids is 2.That means the product call from category is save as current category

check your root category form admin panel you probably add this product to root category also.

Related

How to get parent category id while creating new category in magento?

I am just working on Magento custom extension that is when I create new category belonging to specific category let say "Accessories". Then I want it to display this functionality otherwise I don't want to it to be visible. There is no problem in edit category in edit I can get parent category via Mage::registry('current_category')->getParentCategory()->getId(); but can not get parent id while creating new category.
Kindly give me the best solution guys.
Please try this
<?php
$_categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('level',3);
// $_categories = $_helper->getStoreCategories();
if (count($_categories) > 0){
foreach($_categories as $_category){
$parentId=Mage::getModel('catalog/category')->load($_category->getId())->getParentId();
echo $parentId;
}
}
?>
You may change the level as you need. Let me know if this helped you.

Magento - Get product items attribute in view.phtml

I am trying to load a custom category in the catalog/category/view.phtml to archive this I use:
<?php
$_category = Mage::getModel('catalog/category')->load(47);
$_productCollection = $_category->getProductCollection();
if($_productCollection->count()) {
foreach( $_productCollection as $_product ):
echo $_product->getProductUrl();
echo $this->getPriceHtml($_product, true);
echo $this->htmlEscape($_product->getName());
endforeach;
}
?>
I can load the URL for example, now I want to load a custom attribute for example color:
$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)
This code does not work, I am 100% sure the color attribute is set to show in the category listing and also that the items in this category have this fields fill. I know this because this code works on list.html.
What I am doing wrong? I am working with 1.7.0.2.
The expected result is to show all COLOR attibutes from a custom cateogory in
catalog/category/view.phtml
I can't believe I just found the answer. Because we are not in a regular category listing we need to add the custom attributes to the collection.
Here is the code:
$_productCollection = $_category->getProductCollection()
->addAttributeToSelect('color');
If "color" is in the flat table you should be able to
$_product->getColor();
If this attribute is not in the collection, you can either add it to the flat table by making the attribute filterable, add it in the PHP collection call
$_productCollection = $_category->getProductCollection()
->addAttributeToSelect('color');
Or load the product model to get all of the attributes
$_product = Mage::getModel('catalog/product')->load($_product->getId());
echo $_product->getColor();
Make this attribute visible in listing/frontend
Run Reindexing
foreach( $_productCollection as $_product ):
echo $_product->getProductUrl();
in this code var_dump $_product->getData(); check if this var_dump results in that specific attribute value getting displayed.
Note:
$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)
is not a very efficient way of calling.

how not to show category name in magento breadcrumb?

I am working with a magento website. I have used a featured category to display homepage slider products. So when I click on the product it shows featured as a category in the breadcrumb.
Would it be possible not to show featured in the breadcrumb ? I want category name in the breadcrumb for the rest of categories .
Thanks
Ab
actually not getting your question but you can get some idea from here:
in page/html/breadcrumb.phtml file near line 34-36 change, $_crumbInfo['label'] to $_crumbInfo['title']
<?php elseif($_crumbInfo['last']): ?>
<strong><?php echo $this->htmlEscape($_crumbInfo['title']) ?></strong>
then in catalog/block/breadcrumb.php add 2 lines after
$path = Mage::helper('catalog')->getBreadcrumbPath();
$currentCategory = Mage::registry('current_category');
$metaname = $currentCategory['name'];
and change foreach loop like
foreach ($path as $name => $breadcrumb) {
$breadcrumb['title'] = $metaname;
$breadcrumbsBlock->addCrumb($name, $breadcrumb);
$title[] = $breadcrumb['label'];
}
and check it,
hope you get some idea..
Why no simpler than that?
Try to use CSS. Your category will have an automatic and specific class for it. For example:
<li class="category4">
<strong>ARCHERY HUNTING</strong>
</li>
In this piece of code, I have a category that I created, called "Archery hunting". The code auto-created the class="category4", So, only write on your CSS:
.category4 strong { display: none; }
And it will hide only that category.
Instead of using
$_product->getProductUrl()
to fetch URL, use this:
$_product->unsRequestPath()->getUrlInStore(array('_ignore_category' => true))
Then you need to unset last visited category id at the end of your featured block:
Mage::getSingleton('catalog/session')->setLastVisitedCategoryId('');
This is all because key part for forming breadcrumbs is the following code:
$categoryId = $params->getCategoryId();
if (!$categoryId && ($categoryId !== false)) {
$lastId = Mage::getSingleton('catalog/session')->getLastVisitedCategoryId();
if ($product->canBeShowInCategory($lastId)) {
$categoryId = $lastId;
}
}
basically, current category is determined by either URL params (hence the modified URL call), or through the session object (hence the removal of last visited category id)
so, to recapitulate, in your Featured block, instead of regular productUrl call, use the one I provided, and at the end of your featured product block listing, remove the lastVisitedCategoryId using the code I gave you

Magento - registry and current category

I have a question about Mage::registry and categories: I'm a on a category page, I retrieve current category by Mage::registry('current_category'). I've noticed that it works only for root categories, in fact if I visit a subcategory page I retrieve always the root category with Mage::registry('current_category'). So the question is: is something about backend configuration, cache or something else?
If you are in a template (e.g. catalog/category/view.phtml) you can get the current category with
$this->getCurrentCategory();
If you are in a model, controller or else, try this (found here):
Mage::getModel('catalog/layer')->getCurrentCategory();
However, Mage::registry('current_category') is the normal way to go.
OOB, current_category is set in Mage_Catalog CategoryController::_initCategory() (ref here) and will always be equal to the category currently being viewed.
If your data is different then your app has non-standard functionality or you are seeing cached results.
For all categories:-
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
For Current Category
<?php $currentCategory = Mage::registry('current_category') ?>
Works for me.

Magento Collection: incorrect product category ID in URL

I'm looking to display a featured products collection, based on a featured attribute filter. I get this basically working okay. The part I'm struggling with is that the URL that gets attached to 'getProductUrl()' is wrong.
Comparing the system urls without rewriting them, I noticed that I get this type of url:
catalog/product/view/id/1148/category/6
The category ID at the end is wrong. It shows the parent category ID. If I could get the correct subcategory ID in which those products are assigned, at the end of this URL, then my rewrite would probably work.
Here's my current code, in a featured.phtml template:
<?php
// get all products that are marked as featured
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('featured', array('Yes' => true));
$collection->setOrder('entity_id', 'desc');
$collection->addUrlRewrite($categoryId);
?>
and
<?php foreach ($collection as $_product) : ?>
Can anybody help? Thanks in advance.
Additional notes: the products are in a subcategory but this collection is pulled on the landing page of its parent category and on the home CMS page.
I get: www.sitename.com/product_url_key.html
I want: www.sitename.com/category_path/product_url_key.html

Resources