/** * Get unique category request path * * @param Varien_Object $category * @param string $parentPath * @return string */ public function getCategoryRequestPath($category, $parentPath) { if (version_compare(Mage::getVersion(), '1.7.0.0', '<')) { // The fix below is not compatible withR older Magento versions. // Then only the shell command of this extension is useful. return parent::getCategoryRequestPath($category, $parentPath); } else { $storeId = $category->getStoreId(); $idPath = $this->generatePath('id', null, $category); $suffix = $this->getCategoryUrlSuffix($storeId); if (isset($this->_rewrites[$idPath])) { $this->_rewrite = $this->_rewrites[$idPath]; $existingRequestPath = $this->_rewrites[$idPath]->getRequestPath(); } if ($category->getUrlKey() == '') { $urlKey = $this->getCategoryModel()->formatUrlKey($category->getName()); } else { $urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey()); } $categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId()); if (null === $parentPath) { $parentPath = $this->getResource()->getCategoryParentPath($category); } elseif ($parentPath == '/') { $parentPath = ''; } $parentPath = Mage::helper('catalog/category')->getCategoryUrlPath($parentPath, true, $category->getStoreId()); $requestPath = $parentPath . $urlKey . $categoryUrlSuffix; // PATCH TO FIX MAGENTO BUG THAT DUPLICATES REWRITES WHEN URL_KEY IS EQUAL BETWEEN CATEGORIES $pattern = '/^' . preg_quote($parentPath . $urlKey, '/') . '-\\d{1,}' . preg_quote($categoryUrlSuffix, '/') . '$/'; if (isset($existingRequestPath) && (preg_match($pattern, $existingRequestPath) || $requestPath === $existingRequestPath)) { return $existingRequestPath; } // END OF PATCH if (isset($existingRequestPath) && $existingRequestPath == $requestPath . $suffix) { return $existingRequestPath; } if ($this->_deleteOldTargetPath($requestPath, $idPath, $storeId)) { return $requestPath; } return $this->getUnusedPath($category->getStoreId(), $requestPath, $this->generatePath('id', null, $category)); } }
public function getCategoryRequestPath($category, $parentPath) { /** check for store setting; if YES SHOW PARENT then run parent code - safe for upgrades **/ $configValue = Mage::getStoreConfig('catalog/seo/category_use_parentcategory', Mage::app()->getStore()); if (!$configValue) { return parent::getCategoryRequestPath($category, $parentPath); } $storeId = $category->getStoreId(); $idPath = $this->generatePath('id', null, $category); $suffix = $this->getCategoryUrlSuffix($storeId); if (isset($this->_rewrites[$idPath])) { $this->_rewrite = $this->_rewrites[$idPath]; $existingRequestPath = $this->_rewrites[$idPath]->getRequestPath(); } if ($category->getUrlKey() == '') { $urlKey = $this->getCategoryModel()->formatUrlKey($category->getName()); } else { $urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey()); } $categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId()); if (null === $parentPath) { $parentPath = $this->getResource()->getCategoryParentPath($category); } elseif ($parentPath == '/') { $parentPath = ''; } $parentPath = Mage::helper('catalog/category')->getCategoryUrlPath($parentPath, true, $category->getStoreId()); /** If extension setting enabled then reset parentPath to empty http://magento.stackexchange.com/questions/19471/magento-product-url-with-last-subcategory **/ if ($configValue) { $parentPath = ''; } $requestPath = $parentPath . $urlKey . $categoryUrlSuffix; if (isset($existingRequestPath) && $existingRequestPath == $requestPath . $suffix) { return $existingRequestPath; } if ($this->_deleteOldTargetPath($requestPath, $idPath, $storeId)) { return $requestPath; } return $this->getUnusedPath($category->getStoreId(), $requestPath, $this->generatePath('id', null, $category)); }