Skip to content

Commit

Permalink
Use config from correct store scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmollenhour committed Oct 11, 2022
1 parent af80c2c commit 295d09b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
60 changes: 20 additions & 40 deletions app/code/core/Mage/Catalog/Model/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class Mage_Catalog_Model_Url
*/
const ALLOWED_REQUEST_PATH_OVERFLOW = 10;

const XML_PATH_PRODUCT_USE_CATEGORIES = 'catalog/seo/product_use_categories';
const XML_PATH_CREATE_URL_FOR_DISABLED = 'catalog/seo/create_url_for_disabled';

/**
* Resource model
*
Expand Down Expand Up @@ -78,20 +81,6 @@ class Mage_Catalog_Model_Url
*/
protected $_rewrite;

/**
* Cache for product rewrite suffix
*
* @var array
*/
protected $_productUrlSuffix = [];

/**
* Cache for category rewrite suffix
*
* @var array
*/
protected $_categoryUrlSuffix = [];

/**
* Flag to overwrite config settings for Catalog URL rewrites history maintainance
*
Expand All @@ -106,22 +95,11 @@ class Mage_Catalog_Model_Url
*/
static protected $_categoryForUrlPath;

/**
* @var bool
*/
protected $productUseCategories;

/**
* @var bool
*/
protected $createForDisabled;

public function __construct()
{
$this->productUseCategories = Mage::getStoreConfigFlag('catalog/seo/product_use_categories');
$this->createForDisabled = Mage::getStoreConfigFlag('catalog/seo/create_url_for_disabled');
}

/**
* Adds url_path property for non-root category - to ensure that url path is not empty.
*
Expand Down Expand Up @@ -444,22 +422,23 @@ protected function _refreshCategoryProductRewrites(Varien_Object $category)
*/
public function refreshCategoryRewrite($categoryId, $storeId = null, $refreshProducts = null)
{
if (is_null($refreshProducts)) {
$refreshProducts = $this->productUseCategories;
}
if (is_null($storeId)) {
foreach ($this->getStores() as $store) {
$this->refreshCategoryRewrite($categoryId, $store->getId(), $refreshProducts);
$this->refreshCategoryRewrite($categoryId, $store->getId());
}
return $this;
}
if (is_null($refreshProducts)) {
$refreshProducts = Mage::getStoreConfigFlag(self::XML_PATH_PRODUCT_USE_CATEGORIES, $storeId);
}

$category = $this->getResource()->getCategory($categoryId, $storeId);
if (!$category) {
return $this;
}
$createForDisabled = Mage::getStoreConfigFlag(self::XML_PATH_CREATE_URL_FOR_DISABLED, $storeId);

if (!$this->createForDisabled && !$category->getIsActive()) {
if (!$createForDisabled && !$category->getIsActive()) {
$this->getResource()->clearDisabledCategory($category->getId());
return $this;
}
Expand Down Expand Up @@ -495,8 +474,9 @@ public function refreshProductRewrite($productId, $storeId = null)
}
return $this;
}
$createForDisabled = Mage::getStoreConfigFlag(self::XML_PATH_CREATE_URL_FOR_DISABLED, $storeId);

$product = $this->getResource()->getProduct($productId, $storeId, $this->createForDisabled);
$product = $this->getResource()->getProduct($productId, $storeId, $createForDisabled);
if (!$product) {
// Product doesn't belong to this store - clear all its url rewrites including root one
$this->getResource()->clearProductRewrites($productId, $storeId, []);
Expand All @@ -509,7 +489,7 @@ public function refreshProductRewrite($productId, $storeId = null)
$this->_rewrites = $this->getResource()->prepareRewrites($storeId, '', $productId);

$categories = [];
if ($this->productUseCategories) {
if (Mage::getStoreConfigFlag(self::XML_PATH_PRODUCT_USE_CATEGORIES, $storeId)) {
// List of categories the product is assigned to, filtered by being within the store's categories root
$categories = $this->getResource()->getCategories($product->getCategoryIds(), $storeId);
}
Expand All @@ -530,7 +510,7 @@ public function refreshProductRewrite($productId, $storeId = null)
$excludeCategoryIds = array_keys($categories);

// Product is disabled and in configuration set to not create for disabled - clear all its url rewrites including root one
if (!$this->createForDisabled && $product->getStatus() === Mage_Catalog_Model_Product_Status::STATUS_DISABLED) {
if (!$createForDisabled && $product->getStatus() === Mage_Catalog_Model_Product_Status::STATUS_DISABLED) {
$excludeCategoryIds = [];
}
$this->getResource()->clearProductRewrites($productId, $storeId, $excludeCategoryIds);
Expand All @@ -553,23 +533,23 @@ public function refreshProductRewrites($storeId)
$storeRootCategoryId = $this->getStores($storeId)->getRootCategoryId();
$storeRootCategoryPath = $this->getStores($storeId)->getRootCategoryPath();
$this->_categories[$storeRootCategoryId] = $this->getResource()->getCategory($storeRootCategoryId, $storeId);
$productUseCategories = Mage::getStoreConfigFlag(self::XML_PATH_PRODUCT_USE_CATEGORIES, $storeId);
$createForDisabled = Mage::getStoreConfigFlag(self::XML_PATH_CREATE_URL_FOR_DISABLED, $storeId);

$lastEntityId = 0;
$process = true;

while ($process == true) {
$products = $this->getResource()->getProductsByStore($storeId, $lastEntityId, $this->createForDisabled);
while (true) {
$products = $this->getResource()->getProductsByStore($storeId, $lastEntityId, $createForDisabled);

if (!$products) {
$process = false;
break;
}

$this->_rewrites = $this->getResource()->prepareRewrites($storeId, false, array_keys($products));

$loadCategories = [];

if ($this->productUseCategories) {
if ($productUseCategories) {
foreach ($products as $product) {
foreach ($product->getCategoryIds() as $categoryId) {
if (!isset($this->_categories[$categoryId])) {
Expand All @@ -579,15 +559,15 @@ public function refreshProductRewrites($storeId)
}

if ($loadCategories) {
foreach ($this->getResource()->getCategories($loadCategories, $storeId, $this->createForDisabled) as $category) {
foreach ($this->getResource()->getCategories($loadCategories, $storeId, $createForDisabled) as $category) {
$this->_categories[$category->getId()] = $category;
}
}
}

foreach ($products as $product) {
$this->_refreshProductRewrite($product, $this->_categories[$storeRootCategoryId]);
if ($this->productUseCategories) {
if ($productUseCategories) {
foreach ($product->getCategoryIds() as $categoryId) {
if ($categoryId != $storeRootCategoryId && isset($this->_categories[$categoryId])) {
if (strpos($this->_categories[$categoryId]['path'], $storeRootCategoryPath . '/') !== 0) {
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Catalog/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
<backend_model>adminhtml/system_config_backend_seo</backend_model>
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</create_url_for_disabled>
<save_rewrites_history translate="label">
Expand Down

0 comments on commit 295d09b

Please sign in to comment.