使用SKU过滤Magento的google sitemap

四惠地铁站
四惠地铁站

有些商品不想让进入google的sitemap,我们来看下如何根据sku来过滤Magento的google sitemap。

感谢松泰帮我查了下代码。

1.Magento在前台页面有sitemap页面,页面会显示magento的分类和所有商品,主要是给搜索引擎爬虫来爬的,如果你某些商品不想被抓到,那么不能在这个页面显示出发来。

需要修改的核心文件路径:Mage\Catalog\Block\Seo\Sitemap\Product.php

当然你不能直接修改核心文件,放到code的local文件夹吧。

如下代码,屏蔽sku中带 cus001 的商品

 

    protected function _prepareLayout()
    {
        $collection = Mage::getModel('catalog/product')->getCollection();
        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */

        $collection->addAttributeToSelect('name');
        $collection->addAttributeToSelect('url_key');
        $collection->addStoreFilter();

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
		$collection->addAttributeToFilter('sku',array("nlike"=>'%cus001%'));
        $this->setCollection($collection);

        return $this;
    }

如上的 $collection 添加了 去掉sku中包含 cus001 的商品。

2.需要屏蔽的第二个地方在网站声称的google sitemap的xml文件,此文件也是让搜索引擎抓取的,所以需要屏蔽。

需要的修改的核心文件路径为:Mage\Sitemap\Model\Resource\Catalog\Product.php

    public function getCollection($storeId)
    {
        $products = array();

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

        if (!$store) {
            return false;
        }

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

        $this->_addFilter($storeId, 'visibility', Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds(), 'in');
        $this->_addFilter($storeId, 'status', Mage::getSingleton('catalog/product_status')->getVisibleStatusIds(), 'in');
		$this->_select->where("e.sku not like '%cus001%'");
        $query = $this->_getWriteAdapter()->query($this->_select);
        while ($row = $query->fetch()) {
            $product = $this->_prepareProduct($row);
            $products[$product->getId()] = $product;
        }

        return $products;
    }

上面代码也屏蔽掉了sku中带 cus001 的商品。

转载表明出处:www.hellokeykey.com

《使用SKU过滤Magento的google sitemap》有4个想法

  1. 你好,我的站点后台生成google sitemap.xml 总是出现错误,http://www.buyhiq.com/sitemap.xml :
    This page contains the following errors:

    error on line 2 at column 5712: Extra content at the end of the document
    Below is a rendering of the page up to the first error.

评论已关闭。