Skip to content

Instantly share code, notes, and snippets.

@halfhope
Last active June 2, 2021 18:19
Show Gist options
  • Save halfhope/11d7e59d15dbfe7301dc38c703f7a854 to your computer and use it in GitHub Desktop.
Save halfhope/11d7e59d15dbfe7301dc38c703f7a854 to your computer and use it in GitHub Desktop.
seo_pro auto convert main_category_id
$st = $this->db->query("SELECT product_id FROM oc_product WHERE 1");
// Categories
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = $this->model_catalog_category->getAllCategories();
$filtered_categories = array();
function cmp($a, $b) {
if ($a['level'] == $b['level']) {
return 0;
}
return ($a['level'] < $b['level']) ? -1 : 1;
}
foreach ($data['categories'] as $key => $category) {
$category['name'] = html_entity_decode($category['name']);
$category['level'] = substr_count($category['name'], '>') + 1;
$filtered_categories[(int)$category['category_id']] = $category;
}
$result = array();
foreach ($st->rows as $key => $value) {
$product_id = (int)$value['product_id'];
$categories = $this->model_catalog_product->getProductCategories($product_id);
if ($categories) {
echo 'product_id:' . $product_id . '<br>';
echo 'main_category_id:' . $this->model_catalog_product->getProductMainCategoryId($this->request->get['product_id']) . '<br>';
echo 'categories:<br>';
$cats = array();
foreach ($categories as $key => $category) {
$cats[] = $filtered_categories[$category];
}
uasort($cats, 'cmp');
foreach ($cats as $key => $value) {
if ($value == end($cats)) {
$result[$product_id] = (int)$value['category_id'];
echo "<b>category_id:" . $value['category_id'] . ' name: ' . $value['name'] . '</b><br>';
}else{
echo "category_id:" . $value['category_id'] . ' name: ' . $value['name'] . '<br>';
}
}
echo '<br>';
}
echo '<br>';
}
echo('end testing<br>');
foreach ($result as $product_id => $main_category_id) {
$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' AND category_id = '" . $main_category_id . "'");
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . $main_category_id . "', main_category = 1");
}
die();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment