我坚持在产品系列中获取产品网址.我有这些代码来获得针对特定产品的产品系列.
$_productCollection = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->getCollection();
$_productCollection->addAttributeToFilter('feat_enabled', 1);
$_productCollection->setVisibility(array(2,3,4));
$_productCollection->addUrlRewrite();
$_productCollection->groupByAttribute('entity_id');
$_productCollection->load();
foreach($_productCollection as $_product){
$_product->load($_product->getId());
if($_product->getIsSalable()){
$_name = ltrim(rtrim($_product->getName()));
$_url = $_product->getProductUrl();
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么,如果在每个循环中回显$ _url,则返回的url缺乏.它没有类别名称和子目录名称.正确的网址应该是:
的index.php /类别/子/ itemname.html
但目前它只会返回
index.php/itemname.html
Run Code Online (Sandbox Code Playgroud)
它返回纠正除url之外的数据.如果我将项目分配到类别和子类别,我有管理员的双重检查,我确认我分配了它.
Upon further research, I have found the code below that is very close to what i need.
$_categories = $_product->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load($_categories[0]);
$_url = Mage::getUrl($_category->getUrlPath()).basename($_product->getProductUrl());
Run Code Online (Sandbox Code Playgroud)
这里的问题,$ _url值变成了这样
的index.php /类别/ subcategory.html/itemname.html
子类别名称中包含.html.我的网址中不需要.html,因此点击时该项目会被重定向到正确的页面.
有什么想法解决这个问题?
$_productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addCategoryFilter( $_category )
->addAttributeToFilter('status', array('eq' => 1))
->addUrlRewrite()
->load();
foreach ($_productCollection as $_product):
$_product_url = $_product->getProductUrl();
endforeach;
Run Code Online (Sandbox Code Playgroud)
addUrlRewrite()//魔术