Get Current Top Level Category with Magento - magento

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; ?>

Related

Magento: Switch From Grid view To List View, Without Changing URL

I was wondering if you could guide me how to allow the user to select either list or grid view, without changing the URL of the catalog/category page.
I.e., the page is either www.example.com/category?mode=grid OR www.example.com/category?mode=list but I want to make it just www.example.com/category and show the grid view by default, with the list view being displayed without changing the URL.
I hope you can help
There is no tutorial I guess..you have to do your own code ..And its not a big deal .. Open your list.phtml file in app/design/frontend/default/YOURTHEME/template/catalog/product/
Here you can see, they separate two view mode like this,
<div class="category-products">
<?php echo $this->getToolbarHtml() ?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ol class="products-list" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
..bla.. bla ...
And Grid mode:
<?php else: ?>
<?php // Grid Mode ?>
Here they check the mode like this
<?php if($this->getMode()!='grid'): ?>
Just remove this condition, so that you can load both views, so now just add new css class or id to separate both modes, and manage them by Js like onclik event or something like that ...

Show Product on Search Page - Magento 1.7

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());

Magento - Limit number of products returned on specific pages?

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 :-)

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 - Unable to Refresh Product Stock Status on the Product Page

One of our Vendors has a real time inventory system and we would like to implement it into our site. When a person clicks on the product, it should check the inventory and update as necessary. This works ok at best. The problem is when the product switches to in/out of stock. It updates properly in the backend, but I am unable to get the addtocart button to be added/removed. This is my code for updating the stock:
//$_stockQTY is the realtime inventory result
$stockData = Mage::getModel('cataloginventory/stock_item');
$stockData->loadByProduct($_product->getId());
$stockData->setData('qty', $_stockQTY);
$stockData->setData('is_in_stock',($_stockQTY > 0) ? 1 : 0);
if ($stockData->dataHasChangedFor('qty')) {
$stockData->save();
$_product = Mage::getModel('catalog/product')->load($_product->getId());
}
As you can see, I am force reloading the product when qty is changed. This seems to work for everything but the addtocart button. It shows the previous result (In stock or out of stock before the reload.)
I have 2 questions:
Is there a better way to reload a product other than reassigning it as I am doing above:
$_product = Mage::getModel('catalog/product')->load($_product->getId());
And why is it that everything is updating properly, but the addtocart which uses the same
$_product->isSaleable()
call that our availability, etc uses.
Compare:
<?php if($_product->isSaleable()): ?>
<p class="availability in-stock"><img src="<?php echo $this->getSkinUrl('images/stock.png') ?>"> <span><?php echo $this->__('In stock') ?></span>
...
?>
To
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php endif; ?>
<?php echo $this->getChildHtml('alert_urls') ?> //Only Shows up if addtocart does not.
Refreshing the page will update the product properly, but doing a meta refresh or anything of the sorts is out of the question. I appreciate any advice that could be given as I would like to get this resolved and on to the next task.
Unless I'm misunderstanding your question, it appears the thorn in your side is the stock status index.
Try this:
Mage::getSingleton('cataloginventory/stock_status')->updateStatus($_product->getId());
(I haven't tested this, but it looks like it ought to work)

Resources