-
16-10-2019 - |
题
我有一个分层导航,客户想拥有 <select/>
字段而不是长链接列表。
如果过滤器的名称/ID匹配,我想只是用观察者更改模板。
有更好的想法或建议吗?更改模板的替代方法,也许覆盖/重写块,或者仅更改两个过滤器的块类?
什么事件用于更改模板?
那我有和这里一样的问题 https://stackoverflow.com/questions/14524791/magento-enable-or-disable-a-module-in-code/14529629 因为我有一个观察者,他改变了布局。这只是主题相关。因此,在运行更新之前将主题切换可能是个好主意吗?
解决方案
好的,您需要做的事情:1。覆盖“目录/layer_view”块以设置自己的模板。作为1列布局的一部分,我将分层的导航放在标题区域
<layout>
<catalog_category_layered>
<reference name="header">
<block type="catalog/layer_view" name="mylayered" template="mymodule/catalog/layer/view.phtml"/>
</reference>
</catalog_category_layered>
</layout>
在该模板文件中,您需要为单个元素指定覆盖模板。
<?php if($this->canShowBlock()): ?>
<div class="block block-layered-nav">
<div class="block-title">
<strong><span><?php echo $this->__('Shop By') ?></span></strong>
</div>
<div class="block-content">
<?php echo $this->getStateHtml() ?>
<?php if ($this->getLayer()->getState()->getFilters()): ?>
<div class="actions"><a href="<?php echo $this->getClearUrl() ?>"><?php echo $this->__('Clear All') ?></a></div>
<?php endif; ?>
<?php if($this->canShowOptions()): ?>
<p class="block-subtitle"><?php echo $this->__('Shopping Options') ?></p>
<dl id="narrow-by-list">
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php /* !!! HERE !!! */ ?>
<?php if(some_condition == true){ $_filter->setTemplate('path/to/your/new/filter.phtml'); } ?>
<?php if($_filter->getItemsCount()): ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd><?php echo $_filter->getHtml() ?></dd>
<?php endif; ?>
<?php endforeach; ?>
</dl>
<script type="text/javascript">decorateDataList('narrow-by-list')</script>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
最后,您需要创建下拉级过滤器.phtml。那应该很简单。当我在这个编辑方面遇到麻烦时,这是格式不佳的,但是总体想法就在这里。也将需要一些JavaScript。
<ol>
<li><select>
<?php foreach ($this->getItems() as $_item): ?>
<option value="<?php echo $this->urlEscape($_item->getUrl()); ?>">
<?php if ($_item->getCount() > 0): ?>
<a href="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?></a>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
</option>
<?php endforeach ?>
</select></li>
</ol>
其他提示
您可以使用 core_block_abstract_prepare_layout_after
事件开启 Mage_Catalog_Block_Layer_View
块更改特定属性过滤器的模板。
可能是这样的:
public function yourObserver($observer)
{
$block = $observer->getBlock();
if ($block instanceof Mage_Catalog_Block_Layer_View) {
$block->getChild($yourAttributeCodeGoesHere . '_filter')
->setTemplate('your/template.phtml');
}
}
概括:
每个子滤波器块在分层导航块中都有别名 [attribute_code]_filter
, ,所有这些都在 _prepareLayout()
方法使您可以轻松地修改其模板 core_block_abstract_prepare_layout_after
事件。
另外,您可以在观察者内检查当前主题与预期的主题相同:致电:
$design = Mage::getSingleton('core/design_package')
$design->getPackageName(); // Returns current design package
$design->getTheme('layout'); // Returns current design layout
真诚的,伊万