Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How to retreive getCurrentCategory from product page? 
 
koyan
Jr. Member
 
Total Posts:  29
Joined:  2008-02-28
 

My product pages are displaying a category graphical subheader, determined by the category.

\app\design\frontend\default\default\template\catalog\category\subhdr.phtml

$_imageUrl=$this->getCurrentCategory()->getImageUrl()
<
img src="<?php echo $_imageUrl ?>" width="470" />

via page.xml

<block type="catalog/category_view" name="catalog.category.subhdr" template="catalog/category/subhdr.phtml" />

this works perfectly when the product page is displayed the traditional method
e.g. http://www.site.com/CATEGORY/PRODUCT.html

however, when the product is navigated to via the search result / recently viewed products / send to friend link - basically any link that generates the URL rewrite OMITTING the category from the URL, i get errors. e.g.

“Fatal error: Call to a member function getMetaTitle() on a non-object”

If i change the block type to “catalog/navigation” the page will load, but still cannot access the category id, and fails with this message:
“Fatal error: Call to a member function getImageUrl() on a non-object”

Basically - how can i set / retreive the category ID when i’m in a product page that does not have the category in the url?
(i dont mind defining the first category if the product has been assigned to multiple cats - i just need one for the header)

thanks for any help, i’ve been at this for hours and just cant nail it

 
Magento Community Magento Community
Magento Community
Magento Community
 
koyan
Jr. Member
 
Total Posts:  29
Joined:  2008-02-28
 

Still can’t get to the bottom of this - can you point me in the right direction Magento Team pls?

 
Magento Community Magento Community
Magento Community
Magento Community
 
alkarim
Guru
 
Total Posts:  368
Joined:  2008-04-10
 

hi @koyan, do you want to get the category id ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
koyan
Jr. Member
 
Total Posts:  29
Joined:  2008-02-28
 

yes, ideally i’d like the category image via $this->getCurrentCategory()->getImageUrl()

it appears that when the url does not have the category present (product via search) the category cannot be accessed.
when i try to access the block type “catalog/category_view” i get fatal error (see above), but not when i am in category page, or product page via category

 
Magento Community Magento Community
Magento Community
Magento Community
 
alkarim
Guru
 
Total Posts:  368
Joined:  2008-04-10
 

try this at : app/code/core/Mage/Catalog/Block/Navigation.php

add :

public function drawItemHw($category$aIds{

        $item 
= array();
        if (!
$category->getIsActive()) return $item;

        
$children $category->getChildren();
        
$hasChildren $children && $children->count();
        if (
$hasChildren{
            
foreach ($children as $child{
                
if ($child->getIsActive()) {
                    $tempItem 
$this->drawItemHw($child$aIds);
                    if (!empty(
$tempItem)) $item array_merge($item$tempItem);
                
}
            }            
        } 
else if (in_array($category->getId(), $aIds)) {
            $item[] 
= array($category->getId(), $category->getName(), $this->getCategoryUrl($category));
        
}
        
return $item;
    
}


    
public function getDrawItemHw($ids=null{
        $aIds 
explode(','$ids);
        
$aItem = array();
        
$_categories $this->getStoreCategories();

        foreach (
$_categories as $_category):
            
$tempItem $this->drawItemHw($_category$aIds);
            if (!empty(
$tempItem)) $aItem array_merge($aItem$tempItem);
        endforeach;

        return 
$aItem;
    
}

then you can call it

#param $category_ids, 
#return array of category id, name, and url

$categories Mage::getBlockSingleton('catalog/navigation')->getDrawItemHw($category_ids);

is this what you want ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
koyan
Jr. Member
 
Total Posts:  29
Joined:  2008-02-28
 

i just need to access Mage::getSingleton(’catalog/category’)->getCurrentCategory()->getImageUrl() from my subhdr.phtml file:
it wont allow me to use ‘catalog/category_view’ to access the category - i tried catalog/navigation but cannot access the category from there

<catalog_product_view>
        <!-- 
Mage_Catalog -->
        <
reference name="root">
            <
action method="setTemplate"><template>page/3columns.phtml</template></action>
        </
reference>
        <
reference name="head">
            <
action method="addJs"><script>varien/product.js</script></action>
        </
reference>
        <
reference name="content">
            <
block type="catalog/navigation" name="catalog.category.subhdr" template="catalog/category/subhdr.phtml" />
            <
block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
 
Magento Community Magento Community
Magento Community
Magento Community
 
clorne
Sr. Member
 
Total Posts:  236
Joined:  2008-01-10
London
 

This is just a suggestion but it may help with what I think you are trying to do.
(unfortunately this does involve a a very small change to core code)
If you go to

\app\code\core\Mage\Catalog\Block\Navigation.php

and add

->addAttributeToSelect('image')

to

getCurrentChildCategories() function

$collection->addAttributeToSelect('url_key')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('is_anchor')
            ->
addAttributeToSelect('image'//~~~~
            
->addAttributeToFilter('is_active'1)
            ->
addIdFilter($category->getChildren())
            ->
joinUrlRewrite()
            ->
load();
Keep your xml with the

<block type="catalog/navigation" name="catalog.category.subhdr" template="catalog/category/subhdr.phtml" />

(although you might try putting this in a suitable place in catalog.xml)

and in your subhdr template include something like the following

<?php $_categories=$this->getCurrentChildCategories() ?>
<?php 
if($_categories->count()): ?>
-----html stuff-----
 <!-- 
you can now iterate through $_categories -->
<?php foreach ($_categories as $_category): 
                    
$_imageUrl=$_category->getImageUrl();

<!-- 
you should end up with access to categories and image urls with which you can do with as you want -->
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top