I need to get programmatically the "Target Path" field that I have set in the Magento 1.8 Admin Panel, using Catalog -> URL rewrite management. How do I get it?
1 Answer
The responsible model is Mage_Core_Model_Url_Rewrite which has several possible methods to load:
$rewrite = Mage::getModel('core/url_rewrite')->loadByRequestPath($path)$rewrite = Mage::getModel('core/url_rewrite')->loadByIdPath($path)and the usual$rewrite = Mage::getModel('core/url_rewrite')->load($id)
And then it's a standard attribute getter:
$rewrite->getTargetPath()
-
Thank you friend. Can you let me figure out how to set up the $path variable?Walter Zoccarato– Walter Zoccarato2014-11-14 23:49:20 +00:00Commented Nov 14, 2014 at 23:49
-
I have tried that: $path = $category->getRequestPath(); $rewrite = Mage::getModel('core/url_rewrite')->loadByRequestPath($path); $tmp = $rewrite->getTargetPath(); $path = $category->getPathId(); $rewrite = Mage::getModel('core/url_rewrite')->loadByIdPath($path); $tmp = $rewrite->getTargetPath(); But I can't get the right value.Walter Zoccarato– Walter Zoccarato2014-11-14 23:58:12 +00:00Commented Nov 14, 2014 at 23:58
-
What exactly are you trying to achieve?Fabian Schmengler– Fabian Schmengler2014-11-15 01:33:40 +00:00Commented Nov 15, 2014 at 1:33
-
Thank you. The code you suggest works fine. In order to find the redirection target path URL I have found that you have to call the functions in the following sequence: $path = $category->getRequestPath(); $rewrite = Mage::getModel('core/url_rewrite')->loadByIdPath($path); $targeturl = $rewrite->getTargetPath(); Because, wath is the request_path before the redirection, become the id_path in the final url_revrite.Walter Zoccarato– Walter Zoccarato2014-11-15 11:25:46 +00:00Commented Nov 15, 2014 at 11:25
-
Glad, I could point you in the right direction. In your question you didn't mention that you wanted to find the target path for a given category, so I could not tell you which method is the one you need. Don't forget to accept the answer if it solved your problem.Fabian Schmengler– Fabian Schmengler2014-11-15 21:04:10 +00:00Commented Nov 15, 2014 at 21:04