Resolve:Magento subcategory disable block cache

项目用到了magento 后台,有个需求三级类目太多,在导航栏不能全部显示,显示一个大分类,点击后显示个子全部子分类

(遇到的问题就是block有缓存问题,哪个分类的先显示,其他的分类显示都是同样的子类目,

cache magenment   --->Blocks HTML output--->disabale  xxx这个方法不可取,

分别新建名称不同的block,再displaying setting 下设置CMS Block 选择对应的Block,各自的Block引入的phtml文件的内容都是一样的,代码如下文所示,只是名称不同,感觉有点麻烦)

{{block type="core/template" template="catalog/category/subCategories.phtml"}}

目前看完美的解决方法,只用新建一个phtml文件,各自引用

1:新建subCategories.phtml,放到路径为:

app/design/frontend/base/default/template/catalog/category

文件内容为

getCurrentCategory();
$categories = $category->getCollection()
    ->addAttributeToSelect(array('name', 'thumbnail'))
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter($category->getChildren())
?>

2:app/design/frontend/base/default/layout/catalog.xml 大约77行找到标签添加

 
               
            

3:magento后台,catalog->manageCategories->

Resolve:Magento subcategory disable block cache_第1张图片Resolve:Magento subcategory disable block cache_第2张图片(CMS Block一定要选择默认的 Please select a static block...)

4:自定义Block缓存(以下两个文件如果没有,可copy:app/code/core/下的Mage文件)

app/code/local/Mage/Cms/Block/Block.php

app/code/local/Mage/Cms/Block/Widget/Block.php

两个文件内容都需要增加如下代码

/**
     * Generates a string based on the page url (for example category/product pages) and concatenate the block id to the url
     * Removes the caracters: /, . , &, = and , from this string
     */
    private function generateUrlBasedString($blockId = null)
    {
        $currentUrl = Mage::helper('core/url')->getCurrentUrl();
        $url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
        $path = '_' . $url->getPath();
        
        $path = str_replace('/', '', $path);
        $path = str_replace('.', '', $path);
        $path = str_replace('&', '', $path);
        $path = str_replace(',', '', $path);
        $path = str_replace('=', '', $path);
        
        if(isset($blockId)) {
            $path .= '_' . $blockId;
        }
        
        return $path;
    }

    /**
     * Retrieve values of properties that unambiguously identify unique content
     *
     * @return array
     */
    public function getCacheKeyInfo()
    {
     if ($this->getBlockId()) {
            return array(
                Mage_Cms_Model_Block::CACHE_TAG,
                Mage::app()->getStore()->getId(),
                $this->getBlockId(),
                (int) Mage::app()->getStore()->isCurrentlySecure(),
            );
        } else {
            return parent::getCacheKeyInfo();
        }
    }

5:后台清缓存,重新打开页面,就可以分别看到不同的二级类目显示下属subcategories类目




你可能感兴趣的:(magento,subCategories,magento)