0

in order to retrieve categories path im using the following piece of code in phtml files:

<?php $currentCat = Mage::registry('current_category'); ?>
<?php $exp = explode("/", $currentCat->getPath());?>

I dont understand why,

class Mage_Catalog_Block_Product_List

and its associate catalog/product/list.phtml display the information eg: 1/3/54,

on the other hand,

class Mage_Catalog_Block_Product_List

and its associate catalog/product/view.phtml is not displaying any information. I receive

Fatal error: Call to a member function getPath() on a non-object in \template\page\product.phtml on line 2

I dont know what Im missing, I supposed mage::registry('current_category') was a global variable. and it seems that is not set.

  • does the url have the category name, I believe that effects it. – willboudle Feb 7 '14 at 23:16
  • @willboudle thanks for the answer. currently im using domain.com/index.php/skullcandy-scs-scbp3-5.html , including the category it works but not using directly the .html extension. do you know why? – s_h Feb 7 '14 at 23:25
  • I read on a different page the following: "Also the category will only be set for product pages if the URL contains the relevant category name". – , but why the url generates that. on the other hand, do I have to use custom url routes if I like to mantain the .html extension? thanks – s_h Feb 7 '14 at 23:26
1

Here is the function where it is defined.

It gets the categoryId here:

$categoryId = (int) $this->getRequest()->getParam('id', false);

If that's not in the request path, it won't set 'current_category'.

A product can be in several categories, so when you route to the product directly, it doesn't know the category. There are several ways to find the parent categories of a product, but you will have to find the right method for you. It's easy if you don't put products in more categories but more complex if you do.

Alan Storm's answer here can get you started: https://stackoverflow.com/questions/15735324/get-a-products-parent-category-even-if-it-is-accessed-directly

   protected function _initCatagory()
        {
            Mage::dispatchEvent('catalog_controller_category_init_before', array('controller_action' => $this));
            $categoryId = (int) $this->getRequest()->getParam('id', false);
            if (!$categoryId) {
                return false;
            }

            $category = Mage::getModel('catalog/category')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->load($categoryId);

            if (!Mage::helper('catalog/category')->canShow($category)) {
                return false;
            }
            Mage::getSingleton('catalog/session')->setLastVisitedCategoryId($category->getId());
            Mage::register('current_category', $category);
            try {
                Mage::dispatchEvent(
                    'catalog_controller_category_init_after',
                    array(
                        'category' => $category,
                        'controller_action' => $this
                    )
                );
            } catch (Mage_Core_Exception $e) {
                Mage::logException($e);
                return false;
            }

            return $category;
        }
  • perfect!, thank you – s_h Feb 7 '14 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.