我已经为客户建立了一个Prestashop 1.7网站,我每天都会用脚本导入他的新产品。如果这些产品尚不存在,我会将其分类。我的问题是新创建的类别放在下拉菜单的末尾,按字母顺序显示它们会好得多。我知道我可以通过将它们拖放到后台办公室来做到这一点,但我希望我的脚本能够自动完成。
我已经覆盖Category.php
类进行其他更改,因此我可以编辑此文件。我尝试更改了从ORDER BY
或depth
到position
找到的每个name
条款。它有一些效果,因为类别确实按名称排序,但很多只是从菜单中消失(即按照按位置排序的10个类别中,只有4个按名称排序)。
你知道实现这个目标的方法吗?
答案 0 :(得分:0)
你可以这两种方式做到这一点。 我的方法是在创建菜单时执行此操作,这样就可以按照每种语言进行排序。为此,只需对ps_mainmenu模块使用此覆盖:
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']);
}
另一个选项是在创建菜单时进行排序。但我必须看到当前的导入代码,即便如此也可能更难。
这适用于儿童类别。对于主要类别,有必要覆盖另一个函数。
答案 1 :(得分:0)
问题是查询,例如,如果子类别名称以a开头,父类别以b开头,则prestashop没有显示它,则必须添加主“ order by”,在这种情况下:level_depth。 / p>
我希望我的替代代码会有用:
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);
}
}