階層化されたナビゲーションページのタイトルの変更に例外を追加する

magento.stackexchange https://magento.stackexchange.com/questions/10751

  •  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
    }
役に立ちましたか?

解決

はい、これは1つのオプションです。

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;
}

編集:例を更新し、サンプルコードを追加しました。方法_handlemanufacturermetaは、メーカーに希望するテキストを表示するためにカスタムコードが必要です。ロジックは、getLayersMeta()メソッドがメーカーを検出すると、_handleManufacturersmetaからテキストを返すということです。そうでない場合は、これまでに持っていたテキストを返します。

ただし、ブロックエントリがこれを説明する方法は非常に素晴らしいスタイルではないこと(ローカルコードスペースでコアクラスを上書きする)であることをお勧めします。書き直し、またはより良いオブザーバーを使用することをお勧めします。

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top