Some updated info for Magento 1.9.1
The bug @Vinai pointed out looks solved in this version anyway for other reason the functionality is still broken ( for configurable products )
The problem real problem is probably here Mage_Catalog_Model_Resource_Url
however I don't have time and I dont' want to touch a such delicate part of the core.
Explanation for a workaround:
The entry point is always this classMage_Core_Model_Url_Rewrite_Request
and in particular the method _rewriteDb()
How _rewriteDb()
works:
- First it try to load the request for the current store
(139): $this->_rewrite->loadByRequestPath($requestCases);
- then if I cannot find it (no id) and has a
___from_store
parameter
(142): if (!$this->_rewrite->getId() && $fromStore) {
- try to load a rewrite for the
___from_store
:
(152): $this->_rewrite->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
- if it find it, it use the
id_path
to load the one for the current store:
(159): $this->_rewrite->setStoreId($currentStore->getId())->loadByIdPath($this->_rewrite->getIdPath());
Everything looks fine however there is an issue in the url_rewrite data and so with the index functionality ( at least for configurable products ):
- even if we are switching store and the new store has different url a rewrite at line 139 is loaded.
The problem is that this rewrite points to the wrong id_path
( instead of pointing to the configurable product id it is pointing to one of its simple product id )
Now a workaround is to remove the !$this->_rewrite->getId()
condition and so magento try to find a redirect always when there a $fromstore
parameter
- The best would be to fix the
catalog_url
index and remove the wrong
rewrite it creates.
Here the code for the fast workaround ( you will need to create a module and rewrite Mage_Core_Model_Url_Rewrite_Request
class by your self):
protected function _rewriteDb()
{
if (null === $this->_rewrite->getStoreId() || false === $this->_rewrite->getStoreId()) {
$this->_rewrite->setStoreId($this->_app->getStore()->getId());
}
$requestCases = $this->_getRequestCases();
$fromStore = $this->_request->getQuery('___from_store');
if ($fromStore) {
$stores = $this->_app->getStores(false, true);
if (!empty($stores[$fromStore])) {
/** @var $store Mage_Core_Model_Store */
$store = $stores[$fromStore];
$fromStoreId = $store->getId();
} else {
return parent::_rewriteDb();
}
$this->_rewrite->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
if (!$this->_rewrite->getId()) {
return parent::_rewriteDb();
}
// Load rewrite by id_path
$currentStore = $this->_app->getStore();
$this->_rewrite->setStoreId($currentStore->getId())->loadByIdPath($this->_rewrite->getIdPath());
$this->_setStoreCodeCookie($currentStore->getCode());
$targetUrl = $currentStore->getBaseUrl() . $this->_rewrite->getRequestPath();
$this->_sendRedirectHeaders($targetUrl, true);
}
if (!$this->_rewrite->getId()) {
return parent::_rewriteDb();
}
$this->_request->setAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$this->_rewrite->getRequestPath());
$this->_processRedirectOptions();
return true;
}