Skip to content

Instantly share code, notes, and snippets.

@PululuK
Last active September 10, 2021 15:09
Show Gist options
  • Save PululuK/c1cb4a6228d095b01c3055e14541a97d to your computer and use it in GitHub Desktop.
Save PululuK/c1cb4a6228d095b01c3055e14541a97d to your computer and use it in GitHub Desktop.
Assign all groups in all categories
<?php
require (dirname(__FILE__) . '/config/config.inc.php');
$scriptKey = 'xjkhjiauuhgiu855wbgfb';
if(Tools::getValue('scriptKey') != $scriptKey){
die('403');
}
$categories = (new PrestaShopCollection('Category'))->getResults();
$excludedCategories = [1,2]; // roots categories
$newGroups = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT g.`id_group`
FROM `' . _DB_PREFIX_ . 'group` g'
);
if(!empty($categories)){
foreach ($categories as $categorieObject){
if(!Validate::isLoadedObject($categorieObject) || in_array($categorieObject->id,$excludedCategories)){
continue;
}
$categorieCurrentGroupsId = $categorieObject->getGroups();
// Assing news groups if not exist.
foreach($newGroups as $group){
$groupId = (int) $group['id_group'];
if(!in_array($groupId, $categorieCurrentGroupsId)){
$categorieObject->addGroups([$groupId]);
echo 'Group '.$groupId.' add in categorie '.$categorieObject->id.'<br>';
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment