2012-12-27 86 views
0

我想包括我的Magento網站的sitemap.xml這些頁面,這些頁面不應被搜索引擎編入索引。爲此,我已將Catalog/Category.php Cms/Page.php複製到我的相應app/code/local目錄中,並且正在修改它。PHP代碼從Magento的sitemap.xml中排除特定的類別頁面

我已成功修改Page.php以從我的sitemap.xml中排除特定的CMS頁面。我按照給出的指示here

因爲我不熟悉PHP,我不知道如何更改以適應此解決方案的Category.php文件。

這裏有沒有從sitemap.xml排除任何特定類別的網頁我想我現在的Category.php文件:

<?php 

class Mage_Sitemap_Model_Resource_Catalog_Category extends Mage_Core_Model_Resource_Db_Abstract 
{ 
/** 
* Collection Zend Db select 
* 
* @var Zend_Db_Select 
*/ 
protected $_select; 

/** 
* Attribute cache 
* 
* @var array 
*/ 
protected $_attributesCache = array(); 

/** 
* Init resource model (catalog/category) 
* 
*/ 
protected function _construct() 
{ 
    $this->_init('catalog/category', 'entity_id'); 
} 

/** 
* Get category collection array 
* 
* @param unknown_type $storeId 
* @return array 
*/ 
public function getCollection($storeId) 
{ 
    $categories = array(); 

    $store = Mage::app()->getStore($storeId); 
    /* @var $store Mage_Core_Model_Store */ 

    if (!$store) { 
     return false; 
    } 

    $this->_select = $this->_getWriteAdapter()->select() 
     ->from($this->getMainTable()) 
     ->where($this->getIdFieldName() . '=?', $store->getRootCategoryId()); 
    $categoryRow = $this->_getWriteAdapter()->fetchRow($this->_select); 

    if (!$categoryRow) { 
     return false; 
    } 

    $urConditions = array(
     'e.entity_id=ur.category_id', 
     $this->_getWriteAdapter()->quoteInto('ur.store_id=?', $store->getId()), 
     'ur.product_id IS NULL', 
     $this->_getWriteAdapter()->quoteInto('ur.is_system=?', 1), 
    ); 
    $this->_select = $this->_getWriteAdapter()->select() 
     ->from(array('e' => $this->getMainTable()), array($this->getIdFieldName())) 
     ->joinLeft(
      array('ur' => $this->getTable('core/url_rewrite')), 
      join(' AND ', $urConditions), 
      array('url'=>'request_path') 
     ) 

     ->where('e.path LIKE ?', $categoryRow['path'] . '/%'); 

    $this->_addFilter($storeId, 'is_active', 1); 

    $query = $this->_getWriteAdapter()->query($this->_select); 
    while ($row = $query->fetch()) { 
     $category = $this->_prepareCategory($row); 
     $categories[$category->getId()] = $category; 
    } 

    return $categories; 
} 

/** 
* Prepare category 
* 
* @param array $categoryRow 
* @return Varien_Object 
*/ 
protected function _prepareCategory(array $categoryRow) 
{ 
    $category = new Varien_Object(); 
    $category->setId($categoryRow[$this->getIdFieldName()]); 
    $categoryUrl = !empty($categoryRow['url']) ? $categoryRow['url'] : 'catalog/category/view/id/' . $category->getId(); 
    $category->setUrl($categoryUrl); 
    return $category; 
} 

/** 
* Add attribute to filter 
* 
* @param int $storeId 
* @param string $attributeCode 
* @param mixed $value 
* @param string $type 
* @return Zend_Db_Select 
*/ 
protected function _addFilter($storeId, $attributeCode, $value, $type = '=') 
{ 
    if (!isset($this->_attributesCache[$attributeCode])) { 
     $attribute = Mage::getSingleton('catalog/category')->getResource()->getAttribute($attributeCode); 

     $this->_attributesCache[$attributeCode] = array(
      'entity_type_id' => $attribute->getEntityTypeId(), 
      'attribute_id'  => $attribute->getId(), 
      'table'    => $attribute->getBackend()->getTable(), 
      'is_global'   => $attribute->getIsGlobal(), 
      'backend_type'  => $attribute->getBackendType() 
     ); 
    } 

    $attribute = $this->_attributesCache[$attributeCode]; 

    if (!$this->_select instanceof Zend_Db_Select) { 
     return false; 
    } 

    switch ($type) { 
     case '=': 
      $conditionRule = '=?'; 
      break; 
     case 'in': 
      $conditionRule = ' IN(?)'; 
      break; 
     default: 
      return false; 
      break; 
    } 

    if ($attribute['backend_type'] == 'static') { 
     $this->_select->where('e.' . $attributeCode . $conditionRule, $value); 
    } else { 
     $this->_select->join(
      array('t1_'.$attributeCode => $attribute['table']), 
      'e.entity_id=t1_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.store_id=0', 
      array() 
     ) 
     ->where('t1_'.$attributeCode.'.attribute_id=?', $attribute['attribute_id']); 

     if ($attribute['is_global']) { 
      $this->_select->where('t1_'.$attributeCode.'.value'.$conditionRule, $value); 
     } else { 
      $ifCase = $this->_select->getAdapter()->getCheckSql('t2_'.$attributeCode.'.value_id > 0', 't2_'.$attributeCode.'.value', 't1_'.$attributeCode.'.value'); 
      $this->_select->joinLeft(
       array('t2_'.$attributeCode => $attribute['table']), 
       $this->_getWriteAdapter()->quoteInto('t1_'.$attributeCode.'.entity_id = t2_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.attribute_id = t2_'.$attributeCode.'.attribute_id AND t2_'.$attributeCode.'.store_id=?', $storeId), 
       array() 
      ) 
      ->where('('.$ifCase.')'.$conditionRule, $value); 
     } 
    } 

    return $this->_select; 
} 
} 

編輯:非常感謝盧克在下面他的評論!這是對我有用的東西。我不得不修改代碼,因爲直到if語句之後才定義$ category。

while ($row = $query->fetch()) { 

// Added to exclude specific categories 
if(in_array($this->_prepareCategory($row)->getId(), $this->ignoreCategories)) 
{ 
    continue; 
} 

$category = $this->_prepareCategory($row); 
$categories[$category->getId()] = $category; 
} 

我創建的類ID的類屬性,就像盧克說:

protected $ignoreCategories = array("2","3","16");//Replace the numbers with your category IDs 

回答

1

對於一個快速的解決方案中添加如下代碼,更好的方法是將屬性添加到然後使用類別EAV來檢查它是否應包含在站點地圖輸出中。

對於快速劈,添加的陣列爲一類屬性應被忽略的類別ID,

保護$ ignoreCategories =陣列(「2」,「3」,「16」);

然後靠近getCollection()對這些ID的功能檢查的底部,

而($行= $查詢 - >取()){

// Added to exclude specific categories 
if(in_array($category->getId(), $this->ignoreCategories) 
{ 
    continue; 
} 

    $category = $this->_prepareCategory($row); 
    $categories[$category->getId()] = $category; 

}

+0

由於一個很多盧克!我編輯了我的原始帖子,修改後的代碼適用於我。 – Agrim

相關問題