Magento-自动将空父类别重定向到第一个子类别
我需要将不包含产品的页面重定向到包含产品的第一个子类别URL(如果子类别存在) 因为我只需要在类别不包含任何产品的情况下执行此操作,所以我无法通过.htaccess执行此操作 有人对如何在Magento实现这一点有什么想法吗?Magento-自动将空父类别重定向到第一个子类别,magento,categories,url-redirection,Magento,Categories,Url Redirection,我需要将不包含产品的页面重定向到包含产品的第一个子类别URL(如果子类别存在) 因为我只需要在类别不包含任何产品的情况下执行此操作,所以我无法通过.htaccess执行此操作 有人对如何在Magento实现这一点有什么想法吗? 我通过谷歌搜索的运气不太好。还没有检查过。您应该将其放在view.phtml中 $currentCategory = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $produc
我通过谷歌搜索的运气不太好。还没有检查过。您应该将其放在view.phtml中
$currentCategory = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$productCheck = Mage::getModel('catalog/category')->load($currentCategory)
->getProductCollection();
if (count($productCheck) < 1) {
echo '<script type="text/javascript">
window.location = "' . Mage::getUrl('somewhere') . '"
</script>';
}
$currentCategory=Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$productCheck=Mage::getModel('catalog/category')->load($currentCategory)
->getProductCollection();
如果(计数($productCheck)<1){
回声'
window.location=“”.Mage::getUrl('somewhere')。“
';
}
您可以使用观察者。您可以编辑/创建本地目录模块并尝试以下操作:
1) 在config.xml上定义观察者:
<?xml version="1.0"?>
<config>
<!--(...)-->
<global>
<!--(...)-->
<events>
<catalog_controller_category_init_after>
<observers>
<yourcompany_catalog>
<type>singleton</type>
<class>yourcompany_catalog/observer</class>
<method>categoryRedirect</method>
</yourcompany_catalog>
</observers>
</catalog_controller_category_init_after>
</events>
<!--(...)-->
</global>
<!--(...)-->
</config>
独生子女
贵公司\目录/观察员
类别直接
2) 您的观察者方法可以是这样的:
<?php
class GraspingMagento_SocialShare_Model_Observer
{
/**
* @param Varien_Event_Observer $observer
* @return void
*/
public function categoryRedirect(Varien_Event_Observer $observer)
{
/** @var Mage_Catalog_Model_Category $category */
$category = $observer->getData('category');
if ($category->getProductCollection()->count() > 0) {
// Category has products, no need to redirect
return;
}
/** @var Mage_Catalog_Model_Resource_Category_Collection $childrenCategories */
$childrenCategories = $category->getChildrenCategories();
/** @var Mage_Catalog_CategoryController $controller */
$controller = $observer->getControllerAction();
foreach ($childrenCategories as $childCategory) {
/** @var Mage_Catalog_Model_Category $childCategory */
if ($childCategory->getProductCollection()->count() > 0) {
$controller->getResponse()->setRedirect($childCategory->getData('request_path'));
break;
}
}
}
}