Question

Nous utilisons le code suivant dans app/code/local/Mage/Page/Block/Html/Head.php pour détecter lorsqu'un filtre est actif de la navigation en couches, puis modifier le titre de la page et meta description à partir de cette source 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.' - ' : '';
    }

Cela fonctionne bien et ajoute des données supplémentaires au titre de la page pour toutes les pages avec des filtres de navigation en couches (paramètres de requête) dans l'URL.

Ce que nous aimerions faire est d'ajouter une exception dans la fonction getLayersMeta que les formats le titre de la page différemment lorsque le filtre est fabricant .

Je ne suis pas sûr si je dois ajouter une déclaration elseif pour attraper le paramètre de requête « fabricant » comme if($this->getRequest()->getParam('manufacturer') {}.

Extension de la réponse de @ 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
    }
Était-ce utile?

La 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top