防止在PrestaShop中重复创建类别

时间:2015-08-03 16:56:10

标签: prestashop categories

有一种方法可以防止在prestashop中重复创建类别吗?

那是因为我想使用艺术家名称等类别:

类别:甲壳虫乐队

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

我希望防止管理员插入另类类别“披头士乐队”(不要将产品分散到多个类别)

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

/ 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