1

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
    }

1 Answer 1

2

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.

9
  • It needs to integrate into the code that I posted. I'm unsure how your answer does that...? Thanks.
    – zigojacko
    Nov 13, 2013 at 14:12
  • @GeoffJackson Maybe I misunderstand your question, but this is exactly what my code was trying to do. Insert all your code which you have so far at my comment "Your other logic which handles everyhthing but manufacturer". And insert the logic which you want for manufacturer at "Add manufacturer metas"
    – mpaepper
    Nov 13, 2013 at 15:05
  • Please see my edit to the question. I've included the rest of the function along with your proposed answer (I tried this and the page titles didn't change at all). Please could you check that your answer again as seen in my edit, as integrated into what we have already? Thanks.
    – zigojacko
    Nov 13, 2013 at 15:32
  • @GeoffJackson I updated my answer - hope it helps.
    – mpaepper
    Nov 14, 2013 at 23:16
  • Still persevering with this. Not working very well yet. This gets the ID of the manufacturer too by the way. Somehow needs to include $product->getAttributeText('manufacturer');. Thanks
    – zigojacko
    Nov 15, 2013 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.