I am experiencing an issue where i switched installations and the ids of most of my categories have changed. I need to adjust them. Is there any way it can be changed by running a php script while using magento functions and objects? I tried the following:
<?php // Magento bootstrap use Magento\Framework\App\Bootstrap; use Magento\Framework\App\State; use Magento\Catalog\Model\CategoryFactory; require dirname(__DIR__) . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get(State::class); $state->setAreaCode('adminhtml'); $categoryFactory = $objectManager->get(CategoryFactory::class); // IMPOSTAZIONE CATEGORIE $categoryName = 'BAGNO'; // Categoria da trovare $newCategoryId = 138; // Nuovo id $newParentId = 2; // Nuovo parent id // Load category by name $category = $categoryFactory->create()->getCollection() ->addFieldToFilter('name', $categoryName) ->getFirstItem(); // Mostra errori error_reporting(E_ALL); ini_set('display_errors', 1); // Check if category exists if ($category->getId()) { try { // Start transaction $connection = $category->getResource()->getConnection(); $connection->beginTransaction(); // --lancia aggiornamento-- echo "Prima: "; print_r($category->getData()); echo '<br>'; $attribute_set_id=$category->getData('attribute_set_id'); $created_at=$category->getData('created_at'); $updated_at=$category->getData('updated_at'); $path=$category->getData('path'); $position=$category->getData('position'); $level=$category->getData('level'); $children_count=$category->getData('children_count'); $name=$category->getData('name'); // Set new entity ID and parent ID $category->setData('attribute_set_id', $attribute_set_id); $category->setData('created_at', $created_at); $category->setData('updated_at', $updated_at); $category->setData('path', $path); $category->setData('position', $position); $category->setData('level', $level); $category->setData('children_count', $children_count); $category->setData('name', $name); $category->setData('entity_id',$newCategoryId); $category->setData('parent_id', $newParentId); // Save category $category->save(); // Commit transaction $connection->commit(); echo "Category '$categoryName' updated successfully with new ID: $newCategoryId and parent ID: $newParentId."; } catch (\Exception $e) { // Rollback transaction in case of error $connection->rollBack(); echo "Error updating category: " . $e->getMessage(); } } else { echo "Category '$categoryName' not found."; }
It never results in the data being updated in the database because of an error i get saying Error updating category: Rolled back transaction has not been completed correctly.
Thank you in advance if you are willing to help.