Question

We're using the following code in app/code/local/Mage/Page/Block/Html/Head.php to detect when a filter is active from layered navigation and then amend the page title and meta description based on this sourced from 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.' - ' : '';
    }

This works well and adds additional data to the page title for all pages with layered navigation filters (query parameters) in the URL.

What we'd like to do is add an exception in the getLayersMeta function that formats the page title differently when the filter is manufacturer.

I'm unsure if I need to adding an elseif statement to catch the query parameter 'manufacturer' like if($this->getRequest()->getParam('manufacturer') {}.

Extending with @mpaepper's answer...

/**
 * 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
    }
Was it helpful?

Solution

Yes, this would be one option:

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

EDIT: I updated my example and added sample code. The method _handleManufacturerMeta would need your custom code to show the text you want for manufacturers. The logic is that when the getLayersMeta() method detects a manufacturer, it returns the text from _handleManufacturersMeta. If not, it returns your text which you had so far.

Be advised, though, that the way the block entry describes this is not very nice style (overwriting a core class in local code space) - it would be better to use rewrites or even better some observer.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top