gpt4 book ai didi

用于从 Magento 中的 sitemap.xml 中排除特定类别页面的 PHP 代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:41:32 25 4
gpt4 key购买 nike

我想包含我的 Magento 网站的 sitemap.xml 中不应被搜索引擎编入索引的那些页面。为此,我已将 Catalog/Category.php Cms/Page.php 复制到我相应的 app/code/local 目录中,我正在修改它。

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

由于我不熟悉 PHP,我不知道要使此解决方案适应 Category.php 文件需要做哪些更改。

这是我目前的 Category.php 文件,它不排除我想要从 sitemap.xml 中获取的任何特定类别页面:

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

正如 Luke 所说,我为类别 ID 创建了类属性:

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

最佳答案

要获得快速解决方案,请添加以下代码,更好的方法是向类别 EAV 添加一个属性,然后使用它来检查它是否应包含在站点地图输出中。

为了快速破解,为应该忽略的类别 ID 添加一个数组作为类属性,

protected $ignoreCategories = array("2","3","16");

然后在 getCollection() 函数底部附近检查这些 ID,

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

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

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

关于用于从 Magento 中的 sitemap.xml 中排除特定类别页面的 PHP 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14062323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com