需要帮助Magento Mini-Search

时间:2009-12-10 23:18:38

标签: php search magento e-commerce search-form

我将以下代码提供给Edmond's Ecommerce,将Magento的标准迷你搜索表单转换为更详细的迷你搜索,如亚马逊和eBay中所见。在这个时刻,我能够到达根目录的子类别,但是,我想从子子类别中提取数组。根目录>子类别>子子类别。

试图实现getSubCategories的许多排列,包括以下内容,但不断出现错误。

$category->load(Mage::app()->getStore()->getRootCategoryId()->getSubCategories());

共享的提示没有任何帮助。也许我无法细读它。

if($this->getSubCategories($c)){
foreach($this->getSubCategories($c) as $sc){
foreach($this->getSubCategories($sc) as $ssc){
...
}
}
}

您可能会注意到$ exclude_array选项。子类别数组必须准确,以便我能够排除与迷你搜索无关的类别。任何建议将不胜感激。

<?php
$category = Mage::getModel('catalog/category');
if(is_object(Mage::registry('current_category'))){
    $current_category_path=Mage::registry('current_category')->getPathIds();
}else{
    $current_category_path = array();
}
$category->load(Mage::app()->getStore()->getRootCategoryId());
$children_string = $category->getChildren();
$children = explode(',',$children_string);
$extra_options='';
$exclude_array=array(1,2,3);
foreach($children as $c){
if(in_array($c, $exclude_array)){continue;}
    $selected = (in_array($c, $current_category_path))?'SELECTED':'';
    $extra_options.= '<option value="' . $c . '" ' . $selected . '>' . $category->load($c)->getName() . '</option>' . "\n";    
}
?>

<form id="search_mini_form" action="<?php echo $this->helper('catalogSearch')->getResultUrl() ?>" method="get">
    <fieldset>
        <legend><?php echo $this->__('Search Site') ?></legend>
        <div class="mini-search">
                <?php echo $this->__('I am celebrating my') ?>
            <select name="cat" id="cat" class="input-text">
            <option value="">Any Occassion</option>
            <?= $extra_options ?>
           </select>
            <input id="search" type="text" class="input-text" name="<?php echo $this->helper('catalogSearch')->getQueryParamName() ?>" value="<?php echo $this->helper('catalogSearch')->getEscapedQueryText() ?>" />
            <input class="search-box" type="submit" value="Go" alt="<?php echo $this->__('Search') ?>" />
            <div id="search_autocomplete" class="search-autocomplete"></div>
            <script type="text/javascript">
            //<![CDATA[
                var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__('and looking for...') ?>');
                searchForm.initAutocomplete('<?php echo $this->helper('catalogSearch')->getSuggestUrl() ?>', 'search_autocomplete');
            //]]>
            </script>
        </div>
    </fieldset>
</form>

1 个答案:

答案 0 :(得分:0)

这是一个更简单的解决方案,我已经能够将它组合在一起工作正常。如果有人能够更加浓缩这一点,我们将不胜感激。

<?php
$category = Mage::getModel('catalog/category')->load(enter category id here);
if(is_object(Mage::registry('current_category'))){
    $current_category_path=Mage::registry('current_category')->getPathIds();
}else{
    $current_category_path = array();
}
$children_string = $category->getChildren();
$children = explode(',',$children_string);
$extra_options='';
$exclude_array=array(1,2,3); /* exclude categories */
foreach($children as $c){
if(in_array($c, $exclude_array)){continue;}
    $selected = (in_array($c, $current_category_path))?'SELECTED':'';
    $extra_options.= '<option value="' . $c . '" ' . $selected . '>' . $category->load($c)->getName() . '</option>' . "\n";    
}
?>