在分层导航页面标题的修改中添加例外
-
16-10-2019 - |
题
我们正在使用以下代码 app/code/local/Mage/Page/Block/Html/Head.php
检测何时从分层导航中活跃过滤器,然后根据此来调整页面标题和元描述。 DesignEnd.
public $layerFilters;
public $currentFilters;
public function getLayersMeta(){
$this->layerFilters = '';
$state_block = $this->getLayout()->createBlock("catalog/layer_state");
$this->currentFilters = $state_block->getActiveFilters();
if(!empty($this->currentFilters)) {
foreach($this->currentFilters as $filter) {
$this->layerFilters .= ($this->layerFilters!='') ? ', '.$filter->getName() : $filter->getName();
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $filter->getFilter()->getRequestVar());
foreach($attribute->getSource()->getAllOptions(true, true) as $option) {
if($filter->getValue() == $option['value']) {
$this->layerFilters .= ' - '.strtr($option['label'],array('"'=>"''"));
}
}
}
$this->layerFilters = ($this->layerFilters!='') ? $this->__('Shopping by: ').$this->layerFilters.' - ' : '';
}
这效果很好,并在URL中使用分层导航过滤器(查询参数)的所有页面的页面标题中添加了其他数据。
我们想做的是在 getLayersMeta
过滤器时,以不同的方式格式化页面标题的函数 制造商.
我不确定我是否需要添加 elseif
捕获查询参数“制造商”的声明喜欢 if($this->getRequest()->getParam('manufacturer') {}
.
用 @mpaepper的答案扩展...
/**
* Modifying the page titles for layered navigation (filters) pages.
*
*
*
*/
public $layerFilters;
public $currentFilters;
public $orderBy;
public $currentPage;
public function getLayersMeta(){
$manufacturer = Mage::app()->getRequest()->getParam('manufacturer');
if (!empty($manufacturer)) {
$this->_handleManufacturerMeta();
return false;
}
$this->layerFilters = '';
$state_block = $this->getLayout()->createBlock("catalog/layer_state");
$this->currentFilters = $state_block->getActiveFilters();
if(!empty($this->currentFilters)) {
foreach($this->currentFilters as $filter) {
$this->layerFilters .= ($this->layerFilters!='') ? ', '.$filter->getName() : $filter->getName();
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $filter->getFilter()->getRequestVar());
foreach($attribute->getSource()->getAllOptions(true, true) as $option) {
if($filter->getValue() == $option['value']) {
$this->layerFilters .= ' - '.strtr($option['label'],array('"'=>"''"));
}
}
}
$this->layerFilters = ($this->layerFilters!='') ? $this->__('Shopping by: ').$this->layerFilters.' - ' : '';
}
$this->orderBy = '';
if($this->getRequest()->getParam('order') && $this->getRequest()->getParam('dir')) {
$this->orderBy .= $this->__('Sort by ').$this->__(htmlspecialchars($this->getRequest()->getParam('order')));
$this->orderBy .= (htmlspecialchars($this->getRequest()->getParam('dir')) == 'asc') ? ', '.$this->__('ascending') : ', '.$this->__('descending');
}
if($this->getRequest()->getParam('limit') && is_numeric($this->getRequest()->getParam('limit'))) {
$this->orderBy .= (($this->orderBy!='') ? ', ' : $this->__('show')).' '.$this->getRequest()->getParam('limit').' '.$this->__('per page');
}
if($this->getRequest()->getParam('p') && is_numeric($this->getRequest()->getParam('p'))) {
$this->currentPage = $this->__('Page').' '.$this->getRequest()->getParam('p').' - ';
}
$this->orderBy = ($this->orderBy!='') ? $this->orderBy.' - ' : '';
return $this->layerFilters.$this->orderBy.$this->currentPage;
}
protected function _handleManufacturerMeta() {
// Add manufacturer metas
}
解决方案
是的,这将是一个选择:
public function getLayersMeta() {
$manufacturer = Mage::app()->getRequest()->getParam('manufacturer');
if (!empty($manufacturer)) {
return $this->_handleManufacturerMeta();
}
// Your other logic which handles everything but manufacturer
}
protected function _handleManufacturerMeta() {
$manufacturer = $this->getRequest()->getParam('manufacturer');
$manufacturerValue = '';
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'manufacturer');
foreach($attribute->getSource()->getAllOptions(true, true) as $option) {
if($manufacturer == $option['value']) {
$manufacturerValue = $option['label'];
}
}
return $this->__('My manufacturer is: ').$manufacturerValue;
}
编辑:我更新了示例并添加了示例代码。方法_handleManfucturermeta需要您的自定义代码来显示您想要的制造商的文本。逻辑是,当getlayersmeta()方法检测制造商时,它将返回_handlemanfucturersmeta的文本。如果没有,它将返回您到目前为止所拥有的文本。
不过,请注意,块条目描述的方式不是很好(在本地代码空间中覆盖核心类) - 最好使用重写甚至更好的观察者。