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

文件内容为

<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
    ->addAttributeToSelect(array('name', 'thumbnail'))
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter($category->getChildren())
?>
<div class="subcategories">
    <ul class="clearfix">
    <?php foreach ($categories as $category): ?>
        <li>
            <a href="<?php echo $category->getUrl() ?>"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
                <span><?php echo $category->getName() ?></span></a>
        </li>
    <?php endforeach;?>
</ul>
</div>

2:app/design/frontend/base/default/layout/catalog.xml 大约77行找到<reference name="content">标签添加

 <reference name="content">
            <block type="core/template" template="catalog/category/subCategories.phtml"/>   <!-- add new subCategory -->
            </reference>

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

(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类目




猜你喜欢

转载自blog.csdn.net/qq_25236657/article/details/80689578
今日推荐