Magento:从外部获取类别的相对 URL
我正在构建一个网站,并将 Magento 安装在 /shop/ 子目录中。 我想将顶级类别集成到我的非 Magento 站点的菜单中,以便您可以直接导航到该类别。 为此,我需要类别名称和网址。
Magento 的类别有:
- 模板
- 颜色
- 主题
- 一般
- 其他产品
我首先需要获取模板顶层下方的类别列表,因此我编写了以下代码。 这是我第一次尝试与 Magento 集成,所以要温柔:-):
<?php
require_once dirname(__FILE__).'/shop/app/Mage.php';
umask(0);
Mage::app('default');
$helper = Mage::helper('catalog/category');
$collection = $helper->getStoreCategories();
foreach ($collection as $catalogArray) {
if ($catalogArray->getName() == "Templates") {
foreach (explode(",", $catalogArray->getChildren()) as $category) {
$_category = Mage::getModel('catalog/category')->load($category);
if($_category->getIsActive()) {
$caturl = $_category->getUrl();
$catname = $_category->getName();
}
echo "<pre>";
var_dump($caturl);
echo "</pre>";
echo "<pre>";
var_dump($catname);
echo "</pre>";
}
}
}
?>
这正确地获取了名称和类别,但它返回的 URL 是绝对完整的 URL,例如“http://example.com/shop/templates/theme.html"。
首先,如何从 Magento 检索相对 URL?
其次,只有当我将 web/unsecure/base_url 设置设置为 http://example 时,生成的 URL 似乎才正确.com/shop/。 如果我将其设置为 {{base_url}} (我们在开发过程中更喜欢这样做,因为我们使用基于虚拟托管的 svn 工作副本),则 /shop/ 部分会丢失,即:“http://example.com/templates/theme.html"。
知道为什么吗?
I'm building a website, and installed Magento in the /shop/ subdirectory. I'd like to integrate the top categories into the menu of my non-Magento site, so you can navigate directly into the category. For this I need the category names and url's.
Magento's categories are:
- Templates
- Color
- Theme
- General
- Other products
I first needed to get a list of categories below the Templates top-level, so I wrote the following code. It's my first try with integrating with Magento, so be gentle :-) :
<?php
require_once dirname(__FILE__).'/shop/app/Mage.php';
umask(0);
Mage::app('default');
$helper = Mage::helper('catalog/category');
$collection = $helper->getStoreCategories();
foreach ($collection as $catalogArray) {
if ($catalogArray->getName() == "Templates") {
foreach (explode(",", $catalogArray->getChildren()) as $category) {
$_category = Mage::getModel('catalog/category')->load($category);
if($_category->getIsActive()) {
$caturl = $_category->getUrl();
$catname = $_category->getName();
}
echo "<pre>";
var_dump($caturl);
echo "</pre>";
echo "<pre>";
var_dump($catname);
echo "</pre>";
}
}
}
?>
This correctly gets the names and categories, but the URL's it returns are absolute full URL's, such as "http://example.com/shop/templates/theme.html".
Firstly, how do I retrieve relative URL's from Magento?
Secondly, the generated URL seems to only be correct if I set the web/unsecure/base_url setting to the http://example.com/shop/. If I set this to {{base_url}} instead (which we prefer during development since we use virtual-hosting based svn working copies), the /shop/ part is missing, i.e.: "http://example.com/templates/theme.html".
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以通过系统>中的设置来实现您的所有要求 配置>> >
我尝试的第一件事是将基本网址(如果需要安全和不安全)设置为 / (正斜杠),然后转到 system > 缓存管理> 重建目录 url 重写。
如果这不起作用,请尝试一下设置,相信你可以让它工作。 但请注意,它会使 magento 将相对 URL 放入 google 基本提要等 = 糟糕! 所以不要在生产中这样做。
我的开发副本正在工作,无法从这里访问,因此无法为您尝试。
另外,对于上面的脚本,您可以尝试:(
您可能不需要最后的 html 位,取决于您的配置)
I think you can achieve all of your requirements by playing with the settings in system > configuration > web
First thing I'd try is setting base url (secure and unsecure if needed) to / (forward slash), and then going system > cache management > rebuild catalog url rewrites.
If that doesn't work, play around with the settings, bet you can get it to work. Be warned though, it will make magento put relative urls in google base feeds etc = bad! So don't do it in production.
My dev copy is at work, inacessible from here, so can't try for you.
Also, for your script above, you could try:
(you may not need the html bit at the end, depends on your config)