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.
Related
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.
Currently I have a snippet of code that will forward a user to the product page if they search for a term and only 1 product is associated with that keyword.
<?php if($this->getResultCount() == 1): ?>
<?php $prodId = $this->_productCollection->getAllIds() ?>
<?php $singleProduct = Mage::getModel('catalog/product')->load($prodId) ?>
<?php header('Location: ' . $singleProduct->getProductUrl()) ?>
<?php exit; ?>
<?php elseif($this->getResultCount()): ?>
However, what I want to do now is actually serve up the product and all its details on the results page itself if its the only one with that tag/search term INSTEAD of redirecting to the product page. Im pretty new to php so please bear with me.
Block template is bad place for this. Good place - controller.
Maybe you need rewrite controller for this functionality.
For example/app/code/core/Mage/CatalogSearch/controllers/ResultController.php
In controller your code looks like:
$this->getResponse()->setRedirect($_product->getProductUrl());
I have some specific pages I am linking to from this page - http://www.formagdev1.com/shop-online.html
Top of page, New to the store and Customer Favorites both link to custom pages, but we'd like to limit the # of products displayed on those pages. Exclusive is fine, it can return a page with pagination as it currently does.
So if you click on New to the store, you'll get to a page that dumps an array with all the products with pagination. I would like to limit the amount of products on this page to 25, with no pagination. Same with Customer Favorites page.
I am using Grid Mode only, and the code I have currently building the array is -
<?php $_collectionSize = $_productCollection->count(); ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid">
<?php endif ?>
The issue I am having is, if I just remove the toolbar top and bottom from the custom page, it removes it from ALL list of grid type pages, so the simple idea in this case doesn't seem to be working.
Is there a way to limit the number of products shown to 25 for these 2 specific pages, with NO pagination?
Any ideas?
Thanks!
Bill
Try limit $collection with:
->setPage(1, 25)
But works only if collection not already initialized.
Mage_Catalog_Block_Product_List is the Block that handles the list display. You could add a function here that defines if this is a no pagination category, say function isNoPagination(). You can then edit the catalog/product/list.phtml template to only display the toolbar when !$this->isNoPagination() and set max collection size to 25 when !$this->isNoPagination().
The function isNoPagination could be based on for example getLayer()->getCategoryId().
Thanks for the suggestions everyone. I ended up just doing this using XML in the deisng of the page, workes as I need it to now :-)
How do I include the SKU as well as the product name in the page title (when looking at the product details page)?
Cheers!
In app/design/frontend/default/{your theme Folder}/template/page/html/head.phtml you could try
<?php if ($_product = Mage::registry('current_product')) { ?>
<title><?php echo $_product->getName() . ' ' . $_product->getSku(); ?></title>
<?php }else{ ?>
<title><?php echo $this->getTitle() ?></title>
<?php } ?>
You could also do this using an observer for catalog_controller_product_view see Magento Change Product Page Titles to Include Attributes
If you have a collection already loaded (ie. on product view page)
$_product->getSku();
If for some reason you need to call this from a different template higher up in the chain that isn't already loading the product/collection, you can drop this one:
$_product = Mage::registry('current_product');
$_product->getSku();
look at your design template in catalog/product/view.phtml file you should be able to simply do $_product->getData('sku');
How do I get the current (active) top level category and its subcategories??
I do not want the root category, just the highest level category and all of its subcategories.
If I am in the Women’s Category for instance:
Women
- Apparel
-- Shirts
-- Pants
- Accessories
-- Handbags
-- Jewelry
Even if i am looking at shirts, i would like the category tree to remain the same.
Any help would be greatly appreciated.
To give the precise answer to your precise question "how to get the current category and its subcategories" :
To retrieve the current category :
$_currentCategory = $this->getCurrentCategory();
To retrieve its subcategories :
$_categories = $this->getCurrentChildCategories();
The above are working in a catalog/navigation block.
Now, to get the rendering you are speaking about, I think that a simple navigation block with a good use of CSS would do the trick.
Create a navigation block, let's say in your left column :
Create the template file in your template directory structure. In our example :
/template/catalog/navigation/thetemplate.phtml
Use this code to draw the whole categories/subcategories structure without the hassle of modifying the code (see [1] at the end of the post....)
Inspect the generated code/CSS and you will see that there are all necessary CSS pointers (levelX, active...) allowing you to display or hide pieces of the categories tree and thus showing only the parts you like.
Conclusion: CSS is sufficient to do what you want to do :)
[1] Code :
<?php $_menu = ''?>
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $_menu .= $this->drawItem($_category) ?>
<?php endforeach ?>
<?php if ($_menu): ?>
<div class="THECSS-CONTAINER">
<ul id="THECSS">
<?php echo $_menu; ?>
</ul>
</div>
<?php endif; ?>