Titi2 Posted May 22, 2023 Share Posted May 22, 2023 (edited) Hello Guys! I'm developping a custom module which give me the possibility to create a product programatically. in the Product() object i have to specify catégories of my products with the addToCategories() method. But for now i just have the name of categories. $product->addToCategories() method takes an array of ids categories as argument. So, how can i retrieve the categories ids associated to my categories names please? Seems Prestashop does not provide a function for it. Edited May 22, 2023 by Titi2 (see edit history) Link to comment Share on other sites More sharing options...
Titi2 Posted May 23, 2023 Author Share Posted May 23, 2023 Anyone ? :/ Link to comment Share on other sites More sharing options...
Mediacom87 Posted May 23, 2023 Share Posted May 23, 2023 Hi, have you performed any tests using this function in Category class ? public static function searchByName($idLang, $query, $unrestricted = false, $skipCache = false) Link to comment Share on other sites More sharing options...
Deep Kumar Posted May 24, 2023 Share Posted May 24, 2023 Hi You can use like this Quote $default_lang = Configuration::get('PS_LANG_DEFAULT'); $category_data = Category::searchByName($default_lang, $query, $unrestricted = false, $skipCache = false); if($category_data["id_category"]) { echo "Already Exist"; } else { $c_name = $category_name; $category = new Category(); $category->name = [$default_lang => $c_name]; $category->id_parent = Configuration::get('PS_HOME_CATEGORY'); $category->link_rewrite = [$default_lang => Tools::str2url($c_name)]; $category->add(); } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now