簡體   English   中英

Magento對移動導航類別進行排序

[英]Magento sort mobile navigation categories

情況
我有一個響應式的網上商店。 當用戶在移動電話上時,他仍然可以選擇他要導航到的類別。 這是通過可擴展菜單完成的。 現在,我嘗試按字母順序組織所有類別。 但是我不知道如何實現這一目標。 堆棧上的所有其他答案都不適合我的問題。


如何在展開菜單中按字母順序組織類別?

mobinav.php

<?php 
global $var_yttheme;
if ( $var_yttheme->isHomepage() === FALSE ) {
$homecls='class="nav-home"';
} else {
$homecls='class="nav-home active"';
}
if($var_yttheme->getParam("responsive_menu")=='2'){ ?>
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"><i class="icon-reorder"></i>
</button>
<div id="yt_resmenu_collapse" style="height: 0;" class="nav-collapse collapse">
    <ul class="nav-menu clearfix">
    <li <?php echo $homecls;?>>
        <a <?php echo $homecls;?> href="<?php echo $this->getUrl('') ?>" title="Home"><span>Home</span></a>
    </li>
    <?php foreach ($this->getStoreCategories() as $_category): ?>
        <?php echo $this->drawItem($_category) ?>
    <?php endforeach ?>
    </ul>
    <script type="text/javascript">
        jQuery(document).ready(function($){
            $('#yt_resmenu_collapse .nav-menu > li.parent').each(function(){
                $(this).find('> ul').wrap('<div class="res-wrapnav">');
                $(this).append('<span class="menuress-toggle"></span>');
            });
        });
        jQuery(window).load(function(){
            jQuery('#yt_resmenu_collapse .parent .menuress-toggle').css('height', jQuery('#yt_resmenu_collapse .parent > a').outerHeight());

            jQuery('#yt_resmenu_collapse .parent > .res-wrapnav').each(function(){
                if(jQuery(this).parent().hasClass('open')){
                    jQuery(this).css('height', jQuery(this).children('ul').height());
                }
            });
            jQuery('#yt_resmenu_collapse .parent .menuress-toggle').click(function(){
                if(jQuery(this).parent().hasClass('open')){
                    jQuery(this).parent().removeClass('open');
                    jQuery(this).parent().children('.res-wrapnav').css('height', '0px');
                }else{
                    jQuery(this).parent().addClass('open');
                    jQuery(this).parent().children('.res-wrapnav').css('height', jQuery(this).parent().children('.res-wrapnav').children('ul').height());
                }
            });
        });
    </script>
</div>
<?php
}elseif($var_yttheme->getParam("responsive_menu")=='3'){ ?>
<button type="button" class="btn btn-navbar"><i class="icon-reorder"></i>
</button>
<div id="yt_resmenu_sidebar">
    <ul class="nav-menu clearfix">
    <li <?php echo $homecls;?>>
        <a <?php echo $homecls;?> href="<?php echo $this->getUrl('') ?>"     title="Home"><span>Home</span></a>
    </li>
    <?php foreach ($this->getStoreCategories() as $_category): ?>
        <?php echo $this->drawItem($_category) ?>
    <?php endforeach ?>
    </ul>
    <script type="text/javascript">
        jQuery(document).ready(function($){
            $('body#bd').append('<div class="yt_ressidebar_screennav"><nav id="yt_screennav"><ul class="siderbar-menu"></ul></nav></div>');
            $('#yt_screennav ul.siderbar-menu').html($('#yt_resmenu_sidebar ul.nav-menu').html());

                $('#yt-responsivemenu .btn.btn-navbar').click(function(){
                    if($('body#bd').hasClass('onpen-sidebar')){
                        $('body#bd').removeClass('onpen-sidebar');
                    }else{
                        $('body#bd').addClass('onpen-sidebar');
                    }
                });
        });
    </script>
</div>
<?php
}else{
$options = array();
$category_collection = Mage::getModel('catalog/category')->getCollection();
$category_collection->addAttributeToSelect('*');
foreach ($this->getStoreCategories() as $_category){
    $catIds[] = $_category->getId();
}
$regexp  = "[0-9/]+(".implode("|", $catIds)."){1}[0-9/]?";
if (Mage::helper('catalog/category_flat')->isEnabled()) {           
    $category_collection->addPathsFilter();
}else{
    $category_collection->addPathFilter($regexp);
}
foreach($category_collection as $category){
    $c = new stdClass();
    $c->label = $category->getName();
    $c->value = $category->getId();
    $c->level = $category->getLevel();
    $c->parentid = $category->getParentId();
    $c->url_path = $category->getUrlPath();
    $c->is_active = false;
    if ($this->getCurrentCategory()) {
        if($c->value == array_pop($this->getCurrentCategory()->getPathIds())){
            $c->is_active = true;
        }
    }           
    $cats[$c->value] = $c;
}

foreach($cats as $id => $c){
    if (isset($cats[$c->parentid])){
        if (!isset($cats[$c->parentid]->child)){
            $cats[$c->parentid]->child = array();
        }
        $cats[$c->parentid]->child[] =& $cats[$id];
    }
}
foreach($cats as $id => $c){
    if (!isset($cats[$c->parentid])){
        $stack = array($cats[$id]);
        while( count($stack)>0 ){
            $opt = array_pop($stack);
            $option = array(
                'label' => ($opt->level>2 ? str_repeat('- - ', $opt->level-2) : '') . $opt->label,
                'value' => $opt->value,
                'url_path'  => $opt->url_path,
                'is_active' => $opt->is_active,
            );
            array_push($options, $option);
            if (isset($opt->child) && count($opt->child)){
                foreach(array_reverse($opt->child) as $child){
                    array_push($stack, $child);
                }
            }
        }
    }
}
unset($cats);
?>
<?php 
if ($var_yttheme->isHomepage()===FALSE) {
    $homecls='';
} else {
    $homecls='selected="selected"';
}
?> 
<script type="text/javascript">
function MobileRedirectUrl(){
    window.location.href =    document.getElementById("yt_resmenu_selectbox").value;
}
</script>
<div class="menu-selectbox">
<i class="icon-reorder"></i>
<select id="yt_resmenu_selectbox" onchange="MobileRedirectUrl(this);">
<option value="<?php echo $this->getUrl(); ?>" <?php echo $homecls ?> ><?php echo $this->__('Home') ?></option>
<?php foreach ($options as $item): 
if($item['url_path'] != '/root-catalog' && $item['value'] != '1'){
?>
    <option <?php echo ($item['is_active'])?"selected='selected'":"" ?> value="<?php echo ($item['url_path'])?$this->getBaseUrl().$item['url_path']: $item['value'] ?>"><?php echo $item['label'] ?></option>
<?php 
} endforeach ?>
</select>
</div>
<?php
}
?>

更換

 $category_collection = Mage::getModel('catalog/category')->getCollection();
$category_collection->addAttributeToSelect('*');

 $category_collection = Mage::getModel('catalog/category')->getCollection();
    $category_collection->addAttributeToSelect('*');
    $category_collection->addAttributeToSort('name', 'ASC');

第二種解決方案

function compareByName($options, $option) {
  return strcmp($options["label"], $option["label"]);
}
usort($options, 'compareByName');

print_r($options);

最后的解決方案

$category_collection = Mage::getModel('catalog/category')->getCollection();
$category_collection->addAttributeToSelect('*');
$category_collection->setOrder('name', 'ASC');

取代這個

foreach ($this->getStoreCategories() as $_category){

$helper = Mage::helper('catalog/category');
foreach ($helper->getStoreCategories('name', true, false) as $_category){

更換

$category_collection = Mage::getModel('catalog/category')->getCollection();
$category_collection->addAttributeToSelect('*');

$category_collection = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*');
    ->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_ASC);

要么

$category_collection = Mage::getModel('catalog/category')->getCollection();
    $category_collection->addAttributeToSelect('*');
    $category_collection->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_ASC);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM