Jump to content

Importing products - New category with the same name


Rabatkoder

Recommended Posts

Can someone help me with this code?

I'm trying to make it create a new category when importing csv products.
For now it just puts the products in the existing categories, but i want it to create a new category with the same name as the existing category.

How can i do that when the category name is a string?

if (isset($product->category) AND is_array($product->category) and sizeof($product->category))
           {
               $product->id_category = array(); // Reset default values array

               foreach ($product->category AS $value)
               {
                   if (is_numeric($value))
                   {
                       if (Category::categoryExists(intval($value)))
                           $product->id_category[] = intval($value);
                       else
                       {
                           $categoryToCreate= new Category();
                           $categoryToCreate->id = intval($value);
                           $categoryToCreate->name = self::createMultiLangField($value);
                           $categoryToCreate->active = 1;
                           $categoryToCreate->id_parent = 1; // Default parent is home for unknown category to create
                           if (($fieldError = $categoryToCreate->validateFields(UNFRIENDLY_ERROR, true)) === true AND ($langFieldError = $categoryToCreate->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true AND $categoryToCreate->add())
                               $product->id_category[] = intval($categoryToCreate->id);
                           else
                           {
                               $this->_errors[] = $categoryToCreate->name[$defaultLanguageId].(isset($categoryToCreate->id) ? ' ('.$categoryToCreate->id.')' : '').' '.Tools::displayError('cannot be saved');
                               $this->_errors[] = ($fieldError !== true ? $fieldError : '').($langFieldError !== true ? $langFieldError : '').mysql_error();
                           }
                       }
                   }

                   elseif (is_string($value) AND !empty($value))
                   {

                       $category = Category::searchByName($defaultLanguageId, $value, true);
                       if ($category['id_category'])
                       {
                           $product->id_category[] =    intval($category['id_category']);
                       }
                       else
                       {
                           $categoryToCreate= new Category();
                           $categoryToCreate->name = self::createMultiLangField($value);
                           $categoryToCreate->active = 1;
                           $categoryToCreate->id_parent = 1; // Default parent is home for unknown category to create
                           if (($fieldError = $categoryToCreate->validateFields(UNFRIENDLY_ERROR, true)) === true AND ($langFieldError = $categoryToCreate->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true AND $categoryToCreate->add())
                               $product->id_category[] = intval($categoryToCreate->id);
                           else
                           {
                               $this->_errors[] = $categoryToCreate->name[$defaultLanguageId].(isset($categoryToCreate->id) ? ' ('.$categoryToCreate->id.')' : '').' '.Tools::displayError('cannot be saved');
                               $this->_errors[] = ($fieldError !== true ? $fieldError : '').($langFieldError !== true ? $langFieldError : '').mysql_error();
                           }
                       }
                   }
               }
           }

Link to comment
Share on other sites

There is notice at Back Office import tab: Note that the category import does not support categories of the same name

You may use stored procedure like this:

create table sections_category (sectionid INT, id_category INT);


CREATE PROCEDURE add_category(IN v_sectionid INT, IN v_name VARCHAR(255), IN v_parentsectionid INT)
 MODIFIES SQL DATA
BEGIN
 DECLARE v_id_category, v_id_parent, v_level_depth INT;

if not EXISTS(SELECT 1 from sections_category where sectionid=v_sectionid) then
 select id_category from ishop2_sections_category where sectionid=v_parentsectionid INTO v_id_parent;

 IF v_id_parent IS NULL then
   SET v_id_parent=1;
   SET v_level_depth=0;
 END IF;

 SELECT level_depth from ps_category where id_category=v_id_parent INTO v_level_depth;

 INSERT INTO ps_category (id_parent, level_depth, active, date_add, date_upd)
   VALUES (v_id_parent, v_level_depth+1, 1, now(), now());

 SET v_id_category=LAST_INSERT_ID();

 insert into sections_category (sectionid, id_category) values (v_sectionid, v_id_category);

 insert into ps_category_lang (id_category, id_lang, name, description, link_rewrite, meta_title, meta_keywords, meta_description) 
       select v_id_category, id_lang, v_name,'',v_name,v_name,v_name,v_name from ps_lang where active=1;

 insert into ps_category_group (id_category, id_group) values (v_id_category,1);
 commit;
END IF;
END



then get PrestaShop id_category from table sections_category to have a link to your external category id

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...