Jump to content

[PHP] Add or update product in categories


janholinka

Recommended Posts

Hi all,

 

I have developed custom import of products using core libraries of PrestaShop. Everything works fine when I import new product to one tree of categories. Problem is facing when I want to import new product with more than one tree of categories. 

 

Code with one tree of categories (good):

 

Example of categories from XML

Cars -> Modern -> BMW

 

$ids_created_categories

[1, 2, 3]

$product->id_category_default = (int)current($ids_created_categories);
$product->save();
$product->updateCategories($ids_created_categories); 

Same code but more ids of categories (bad):

 

Example of categories from XML

Cars -> Modern -> BMW

Toys -> Garden -> Sandbox

Sport -> Football -> Balls

 

$ids_created_categories

 

[1, 2, 3, 8, 9, 10, 20, 21, 22]

 

Code above does not work for this case! (but should be)

 

I have to add product to some category first and then others like below:

$product->id_category_default = (int)current($ids_created_categories);
$product->save();   
$product->updateCategories(array((int)current($ids_created_categories)));
$product->updateCategories($ids_created_categories); // I have to use method updateCategories twice 

PS: Same problem with $product->addToCategories() method.

 

Can someone help me? Maybe I did something wrong.

 

Thanks,

 

Regards

 

Link to comment
Share on other sites

Hi,

 

If you read closely updateCategories

   public function updateCategories($categories, $keeping_current_pos = false)
    {
        if (empty($categories))
            return false;

        $result = Db::getInstance()->executeS('
            SELECT c.`id_category`
            FROM `'._DB_PREFIX_.'category_product` cp
            LEFT JOIN `'._DB_PREFIX_.'category` c ON (c.`id_category` = cp.`id_category`)
            '.Shop::addSqlAssociation('category', 'c', true, null, true).'
            WHERE cp.`id_category` NOT IN ('.implode(',', array_map('intval', $categories)).')
            AND cp.id_product = '.$this->id
        );

        // if none are found, it's an error
        if (!is_array($result))
            return false;

        foreach ($result as $categ_to_delete)
            $this->deleteCategory($categ_to_delete['id_category']);

        if (!$this->addToCategories($categories))
            return false;

        SpecificPriceRule::applyAllRules(array((int)$this->id));
        return true;
    }

All the previous categories ($result) are removed before adding the new one ($categories).

 

Why not use addToCategories directly ?

$product->addToCategories([1, 2, 3, 8, 9, 10, 20, 21, 22]);

 

Link to comment
Share on other sites

Thanks for reply and tip with addToCategories() method. 

 

Now it is working fine. Problem wasn't with type of method I have used but with duplicate ids of categories in array. 

 

Now, before adding product to categories I use function array_unique().

 

 

Thanks for help.

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