Jump to content

[SOLVED] prevent duplicate creation of categories


Recommended Posts

there is a way to prevent duplicate creation of categories? 

 

 

That's because I want use categories like artist name:

 

category: The Beatles

 

product1: Please Please Me 

product2: With the Beatles

product3: A Hard Day's Night 

etc

 

 

I want prevent that admin could insert another category like this "the beatles" (for not disperse the products across multiple categories)

 

thanks

 

 

 

 

SOLVED: 

 

Adding new file called AdminCategoriesController.php in/prestashop/override/controllers/admin/

<?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;
}
}

You can change the user message for duplicate entry here by change "conf" parameter

Tools::redirectAdmin(self::$currentIndex.'&id_category='.(int)Configuration::get('PS_ROOT_CATEGORY').'&token='.Tools::getAdminTokenLite('AdminCategories').'&conf=1234'); 

do not forget to delete class_index.php inside /prestashop/cache

Edited by Dottor M (see edit history)
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...