Matteo Enna
Matteo Enna

Reputation: 1301

Add category when I create a product on PrestaShop

I am creating an import module products on PrestaShop 1.6. the product is created without problems but the categories are not included.

I used:

$product->id_category = array();
foreach($arr_cat_full as $cat){                          
    $category = Category::searchByName(1, trim($cat), true);
    $product->id_category[] = (int)$category['id_category'];                       
}

If I enter:

var_dump($category['id_category']);

The result is correct.

Upvotes: 2

Views: 2091

Answers (1)

PixelWeb
PixelWeb

Reputation: 568

You must use addToCategories($categories = array()) function of Product class.

to use in this way:

$array_cc = array();
$product->id_category = array();
foreach($arr_cat_full as $cat){                          
        $category = Category::searchByName(1, trim($cat), true);
        $array_cc = (int)$category['id_category'];                       
}

$product->add();
$product->addToCategories($array_cc);

Good luck.

Upvotes: 3

Related Questions