Jump to content

Get categories IDs by categories names


Titi2

Recommended Posts

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 by Titi2 (see edit history)
Link to comment
Share on other sites

  • Titi2 changed the title to Get categories IDs by categories names

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

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