Vinai / groupscatalog2

Magento extension to enable you to hide categories and products from customers depending on their customer group. This is a Magento 1.6 and newer compatible version of the Netzarbeiter Customer Groups Catalog extension.
139 stars 60 forks source link

{{blocks}}, category settings and parent/root category settings #2

Closed bdgeorge closed 12 years ago

bdgeorge commented 12 years ago

If I have a root category that is visible only to a certain group, but then I choose to remove all restrictions from a sub-category under it I'm finding that I still can' t access that subcategory from within a custom layout or {{block}} rule. I can access the sub-category via it's URL but I can't access it from XML or {{block}} syntax.

So as an example, say I have a CMS page which contains some new products called up like this:

{{block type="catalog/product_list" category_id="262" template="catalog/guest/new_products_homepage.phtml"}}

Then I set the root category to be visible only to logged in users and this new products category is set to be visible to all. If I then visit the CMS page that holds the block it triggers an "Invalid category" exception.

    a:5:{i:0;s:17:"Invalid category.";i:1;s:3685:"#0 /var/data/mysite/web/app/code/core/Mage/Catalog/Model/Layer.php(191): 
Mage::throwException('Invalid categor...')

In the stack trace there's no sign of Groups catalog , but I can work around the problem by disabling Groups catalog so I'm sure it's coming from here.

My current solution is to set a registry key from my layout xml files that I then use to disable groups catalog on specific pages, so a tweak in Data.php like this

        public function isModuleActive($store = null, $checkAdmin = true)
    {
        $store = Mage::app()->getStore($store);
        if ($checkAdmin && $store->isAdmin())
        {
            return false;
        }
        //benz001
        //Check the registry for a disable flag and honour it
        if(Mage::registry('wh_disable_groups_ext') == 1){           
            //We don't care about stores here as we expect this to come from a store level template
            return false;
        }   
        //end benz001

        $setting = $this->getConfig('is_active', $store);
        return (bool) $setting;
    }

Then I create a new block to set/unset registry keys so I can trigger it from a theme.

So I'm not sure if this is a bug or a feature request: 1) having an exception triggering response to inserting a {{block}} or seems like a bug, 2) being able to deliberately 'punch holes' in the groups catalog system from the layout files feels very much like a useful new feature that themes can make use of.

Vinai commented 12 years ago

Thanks for your report. The issue occurs because the layer model uses the root category as the default current category. A bit longer explanation:

  1. The product list block uses the layer model to fetch the product collection
  2. The layer model always sets the current category.
  3. If there is no current category set, the layer model uses the store root category as default
  4. The store root category is loaded
  5. This fails because that category is hidden by the rules you set

The exception is triggered because in effect a non-existant root category is assigned to the current store.

There is no easy way to change this behavior, besides making it impossible to hide root categories. In most cases this would be no problem, since the root category is rarely used in stores besides it's function as a container for all normal categories. It would certainly break your setup since you seem to rely on having the root hidden.

I suggest you rework your rules so you don't hide the root category, but instead rely only on regular categories.

bdgeorge commented 12 years ago

Thanks Vinai, hiding the root category seemed like the most elegant architecture for a members only store, I'll see how else I can go about this.

Vinai commented 12 years ago

The problem only occurs if the root category is hidden and you the try to use the product list block. If there where an easy way to fix this I would be happy to do it, but so far I can't think of a clean way to do so.