Php 在Prestashop顶部菜单中按字母顺序排列类别

Php 在Prestashop顶部菜单中按字母顺序排列类别,php,prestashop,prestashop-1.7,Php,Prestashop,Prestashop 1.7,我已经为一个客户建立了一个Prestashop 1.7网站,我每天都用脚本导入他的新产品。这些产品被归入我创建的类别,如果它们还不存在的话。我的问题是,新创建的类别放在下拉顶部菜单的末尾,最好按字母顺序显示。我知道我可以在后台通过拖放它们来实现这一点,但我希望我的脚本能够自动完成 我已经重写了Category.php类以进行其他更改,以便编辑此文件。我试图通过子句将我从深度或位置找到的每个顺序更改为名称。它产生了一些影响,因为类别确实是按名称排序的,但许多类别只是从菜单中消失了(即,在按位置排序

我已经为一个客户建立了一个Prestashop 1.7网站,我每天都用脚本导入他的新产品。这些产品被归入我创建的类别,如果它们还不存在的话。我的问题是,新创建的类别放在下拉顶部菜单的末尾,最好按字母顺序显示。我知道我可以在后台通过拖放它们来实现这一点,但我希望我的脚本能够自动完成

我已经重写了
Category.php
类以进行其他更改,以便编辑此文件。我试图通过子句将我从
深度
位置
找到的每个
顺序更改为
名称
。它产生了一些影响,因为类别确实是按名称排序的,但许多类别只是从菜单中消失了(即,在按位置排序的10个类别中,只有4个仍然按名称排序)

你知道实现这一目标的方法吗?

你可以用两种方法。 我的方法是在创建菜单时这样做,这样它就可以用每种语言进行排序。为此,只需对ps_主菜单模块使用此覆盖:

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

class Ps_MainMenuOverride extends Ps_MainMenu implements WidgetInterface
{
    protected function generateCategoriesMenu($categories, $is_children = 0)
    {
        $categories = $this->sortCategories($categories);
        return parent::generateCategoriesMenu($categories, $is_children);
    }

    public function sortCategories($categories)
    {

        uasort($categories, 'cmpcat');

        foreach($categories as $k => $category)
        {
            if (isset($category['children']) && !empty($category['children'])) {
                $children = $this->sortCategories($category['children']);
                $categories[$k]['children'] = $children;
            }
        }
        return $categories;
    }
}

function cmpcat($a, $b) {
    return strcmp($a['name'], $b['name']);
}
另一个选项是在创建菜单时进行排序。但我必须查看当前的导入代码,即使这样也可能会更加困难


这是针对儿童类别的。对于主类别,有必要覆盖另一个函数。

问题在于查询,例如,如果子类别名称以a开头,父类别名称以b开头,则prestashop不显示该名称,则必须添加一个主“order by”,在本例中为:level\U depth

我希望我的覆盖代码会有用:

    use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

    class Ps_MainMenuOverride extends Ps_MainMenu  implements WidgetInterface
    {
        protected function makeMenu()
        {
            $root_node = $this->makeNode([
                'label' => null,
                'type'  => 'root',
                'children' => []
            ]);

            $menu_items = $this->getMenuItems();
            $id_lang = (int)$this->context->language->id;
            $id_shop = (int)Shop::getContextShopID();

            foreach ($menu_items as $item) {
                if (!$item) {
                    continue;
                }
                preg_match($this->pattern, $item, $value);
                $id = (int)substr($item, strlen($value[1]), strlen($item));

                switch (substr($item, 0, strlen($value[1]))) {
                    case 'CAT':
                        $categories = $this->generateCategoriesMenu(
                            Category::getNestedCategories($id, $id_lang, false, $this->user_groups,true,'',' ORDER BY c.`level_depth`,cl.`name` ASC ')
                        );
                        $root_node['children'] = array_merge($root_node['children'], $categories);
                        break;

                    case 'PRD':
                        $product = new Product((int)$id, true, (int)$id_lang);
                        if ($product->id) {
                            $root_node['children'][] = $this->makeNode([
                                'type' => 'product',
                                'page_identifier' => 'product-' . $product->id,
                                'label' => $product->name,
                                'url' => $product->getLink(),
                            ]);
                        }
                        break;

                    case 'CMS':
                        $cms = CMS::getLinks((int)$id_lang, array($id));
                        if (count($cms)) {
                            $root_node['children'][] = $this->makeNode([
                                'type' => 'cms-page',
                                'page_identifier' => 'cms-page-' . $id,
                                'label' => $cms[0]['meta_title'],
                                'url' => $cms[0]['link']
                            ]);
                        }
                        break;

                    case 'CMS_CAT':
                        $root_node['children'][] = $this->generateCMSCategoriesMenu((int)$id, (int)$id_lang);
                        break;

                    // Case to handle the option to show all Manufacturers
                    case 'ALLMAN':
                        $children = array_map(function ($manufacturer) use ($id_lang) {
                            return $this->makeNode([
                                'type' => 'manufacturer',
                                'page_identifier' => 'manufacturer-' . $manufacturer['id_manufacturer'],
                                'label' => $manufacturer['name'],
                                'url' => $this->context->link->getManufacturerLink(
                                    new Manufacturer($manufacturer['id_manufacturer'], $id_lang),
                                    null,
                                    $id_lang
                                )
                            ]);
                        }, Manufacturer::getManufacturers());

                        $root_node['children'][] = $this->makeNode([
                            'type' => 'manufacturers',
                            'page_identifier' => 'manufacturers',
                            'label' => $this->trans('All brands', array(), 'Modules.Mainmenu.Admin'),
                            'url' => $this->context->link->getPageLink('manufacturer'),
                            'children' => $children
                        ]);
                        break;

                    case 'MAN':
                        $manufacturer = new Manufacturer($id, $id_lang);
                        if ($manufacturer->id) {
                            $root_node['children'][] = $this->makeNode([
                                'type' => 'manufacturer',
                                'page_identifier' => 'manufacturer-' . $manufacturer->id,
                                'label' => $manufacturer->name,
                                'url' => $this->context->link->getManufacturerLink(
                                    $manufacturer,
                                    null,
                                    $id_lang
                                )
                            ]);
                        }
                        break;

                    // Case to handle the option to show all Suppliers
                    case 'ALLSUP':
                        $children = array_map(function ($supplier) use ($id_lang) {
                            return $this->makeNode([
                                'type' => 'supplier',
                                'page_identifier' => 'supplier-' . $supplier['id_supplier'],
                                'label' => $supplier['name'],
                                'url' => $this->context->link->getSupplierLink(
                                    new Supplier($supplier['id_supplier'], $id_lang),
                                    null,
                                    $id_lang
                                )
                            ]);
                        }, Supplier::getSuppliers());

                        $root_node['children'][] = $this->makeNode([
                            'type' => 'suppliers',
                            'page_identifier' => 'suppliers',
                            'label' => $this->trans('All suppliers', array(), 'Modules.Mainmenu.Admin'),
                            'url' => $this->context->link->getPageLink('supplier'),
                            'children' => $children
                        ]);
                        break;

                    case 'SUP':
                        $supplier = new Supplier($id, $id_lang);
                        if ($supplier->id) {
                            $root_node['children'][] = $this->makeNode([
                                'type' => 'supplier',
                                'page_identifier' => 'supplier-' . $supplier->id,
                                'label' => $supplier->name,
                                'url' => $this->context->link->getSupplierLink(
                                    $supplier,
                                    null,
                                    $id_lang
                                )
                            ]);
                        }
                        break;

                    case 'SHOP':
                        $shop = new Shop((int)$id);
                        if (Validate::isLoadedObject($shop)) {
                            $root_node['children'][] = $this->makeNode([
                                'type' => 'shop',
                                'page_identifier' => 'shop-' . $id,
                                'label' => $shop->name,
                                'url' => $shop->getBaseURL(),
                            ]);
                        }
                        break;
                    case 'LNK':
                        $link = Ps_MenuTopLinks::get($id, $id_lang, $id_shop);
                        if (!empty($link)) {
                            if (!isset($link[0]['label']) || ($link[0]['label'] == '')) {
                                $default_language = Configuration::get('PS_LANG_DEFAULT');
                                $link = Ps_MenuTopLinks::get($link[0]['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());
                            }
                            $root_node['children'][] = $this->makeNode([
                                'type' => 'link',
                                'page_identifier' => 'lnk-' . Tools::str2url($link[0]['label']),
                                'label' => $link[0]['label'],
                                'url' => $link[0]['link'],
                                'open_in_new_window' => $link[0]['new_window']
                            ]);
                        }
                        break;
                }
            }

            return $this->mapTree(function ($node, $depth) {
                $node['depth'] = $depth;
                return $node;
            }, $root_node);
        }

    }

你好,我试过了。我复制了代码并粘贴在ps_main menu.php文件的末尾。但我想我必须在其他地方更改代码,因为它不适合我。我还需要改变什么?谢谢你的帮助@你是否创建了覆盖文件并清除了缓存?在1.7中,您需要删除文件夹app/cache/dev和/或app/cache/prod。您使用的是不使用默认功能的自定义主题吗?@sadlyblue谢谢!我用错文件了。我不知道覆盖文件夹。这篇文章为我指出了正确的方向:现在它可以工作了