防止在 PrestaShop 中重复创建类别

prevent duplicate creation of categories in PrestaShop

有没有办法防止在 prestashop 中重复创建类别?

那是因为我想使用艺术家姓名这样的类别:

类别:甲壳虫乐队

product1:拜托拜托我 product2: 披头士乐队 product3: 艰难的一天之夜 等等

我想防止管理员像这样插入另一个类别"the beatles"(为了不将产品分散到多个类别中)

覆盖 AdminCategoriesController 上的 processAdd() 函数以添加您的名称控件。 使用类别 Class 中的 searchByName() 函数来执行此操作。

正在 /prestashop/override/controllers/admin/

中添加名为 AdminCategoriesController.php 的新文件
<?php 
class AdminCategoriesController extends AdminCategoriesControllerCore{

public function processAdd()
{

    $id_category = (int)Tools::getValue('id_category');
    $id_parent = (int)Tools::getValue('id_parent');

    // if true, we are in a root category creation
    if (!$id_parent)
    {
                $_POST['is_root_category'] = $_POST['level_depth'] = 1;
                $_POST['id_parent'] = $id_parent = (int)Configuration::get('PS_ROOT_CATEGORY');
    }

    if ($id_category)
    {
        if ($id_category != $id_parent)
        {
            if (!Category::checkBeforeMove($id_category, $id_parent))
                $this->errors[] = Tools::displayError('The category cannot be moved here.');
        }
        else
            $this->errors[] = Tools::displayError('The category cannot be a parent of itself.');
    }


            $categoryName = (!empty($_POST['name_1'])) ? $_POST['name_1'] : $_POST['name_2'];
            $duplicated = Category::searchByName(0, $categoryName, true);

            if($duplicated){
                Tools::redirectAdmin(self::$currentIndex.'&id_category='.(int)Configuration::get('PS_ROOT_CATEGORY').'&token='.Tools::getAdminTokenLite('AdminCategories').'&conf=4');
                return ;
            }
    $object = parent::processAdd();

    //if we create a you root category you have to associate to a shop before to add sub categories in. So we redirect to AdminCategories listing
    if ($object && Tools::getValue('is_root_category'))
        Tools::redirectAdmin(self::$currentIndex.'&id_category='.(int)Configuration::get('PS_ROOT_CATEGORY').'&token='.Tools::getAdminTokenLite('AdminCategories').'&conf=3');
    return $object;
}
}

您可以通过更改"conf" 参数

来更改重复输入的用户消息
Tools::redirectAdmin(self::$currentIndex.'&id_category='.(int)Configuration::get('PS_ROOT_CATEGORY').'&token='.Tools::getAdminTokenLite('AdminCategories').'&conf=1234');

别忘了删除/prestashop/cache

里面的class_index.php