Magento - Remove Multiple Product URL Rewrites

时间:2016-08-31 17:04:34

标签: magento url url-rewriting

In my CE-1.9.0 store my products currently create the following url rewrites:

http://example.com/product-name
http://example.com/category/product-name
http://example.com/another-category/subcategory/product-name

However in my store I want to be able to stop creating the rewrites for the category versions. I have System > Configuration > Catalog > Catalog > Use Categories Path for Product URLs set to No so these extra rewrites are unnecessary. Plus my store has around 500,000 products and counting so these extra url's are taking up a lot of space in the core_url_rewrite` table.

So my aim is to only be left with these versions:

http://example.com/product-name

If I truncate the core_url_rewrite table and reindex then only these versions get created so it is only when new products are created. My catalog is so large so I cannot keep truncated and rebuilding the table from scratch as the rebuilding takes several hours.

With help from this question I have found the file that is responsible for creating the rewrites when a product is created. It is:

app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php

However the answer is no longer valid as the code has changed in the core file.

In this file on line #538 there is the _afterLoad function it is the line

$this->_addUrlRewrite($this->_urlRewriteCategory);

And this function _addUrlRewrite however i've tried changing a lot inside this function but have had no luck. The function in full is:

protected function _addUrlRewrite()
{
    $urlRewrites = null;
    if ($this->_cacheConf) {
        if (!($urlRewrites = Mage::app()->loadCache($this->_cacheConf['prefix'] . 'urlrewrite'))) {
            $urlRewrites = null;
        } else {
            $urlRewrites = unserialize($urlRewrites);
        }
    }

    if (!$urlRewrites) {
        $productIds = array();
        foreach($this->getItems() as $item) {
            $productIds[] = $item->getEntityId();
        }
        if (!count($productIds)) {
            return;
        }

        $select = $this->_factory->getProductUrlRewriteHelper()
            ->getTableSelect($productIds, $this->_urlRewriteCategory, Mage::app()->getStore()->getId());

        $urlRewrites = array();
        foreach ($this->getConnection()->fetchAll($select) as $row) {
            if (!isset($urlRewrites[$row['product_id']])) {
                $urlRewrites[$row['product_id']] = $row['request_path'];
            }
        }

        if ($this->_cacheConf) {
            Mage::app()->saveCache(
                serialize($urlRewrites),
                $this->_cacheConf['prefix'] . 'urlrewrite',
                array_merge($this->_cacheConf['tags'], array(Mage_Catalog_Model_Product_Url::CACHE_TAG)),
                $this->_cacheLifetime
            );
        }
    }

    foreach($this->getItems() as $item) {
        if (empty($this->_urlRewriteCategory)) {
            $item->setDoNotUseCategoryId(true);
        }
        if (isset($urlRewrites[$item->getEntityId()])) {
            $item->setData('request_path', $urlRewrites[$item->getEntityId()]);
        } else {
            $item->setData('request_path', false);
        }
    }
}

0 个答案:

没有答案