Open henriquekieckbusch opened 2 years ago
Hello @henriquekieckbusch how did you solve this ? I'm struggling with the same problems.
The way I did is not the best one, but it works...
<type name="Akeneo\Connector\Job\Category"> <plugin name="akeneo-category-plugin" type="My_Name\Akeneo\Plugin\Job\Category" sortOrder="10"/> </type>
And the Plugin is:
` use Akeneo\Connector\Helper\Import\Entities; use Akeneo\Connector\Job\Category as CategoryOriginal; use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\Exception\LocalizedException; use Zend_Db_Expr as Expr;
class Category {
private CategoryCollectionFactory $categoryCollectionFactory;
private Entities $entitiesHelper;
public function __construct(
Entities $entitiesHelper,
CategoryCollectionFactory $categoryCollectionFactory
) {
$this->entitiesHelper = $entitiesHelper;
$this->categoryCollectionFactory = $categoryCollectionFactory;
}
public function afterSetStructure(CategoryOriginal $subject) : void
{
/** @var AdapterInterface $connection */
$connection = $this->entitiesHelper->getConnection();
$akeneoConnectorTable = $this->entitiesHelper->getTable('akeneo_connector_entities');
$tmp_table = $this->entitiesHelper->getTable('tmp_akeneo_connector_entities_category');
$listCategories = $this->categoryCollectionFactory->create();
$listCategories->addNameToResult();
/** @var \Magento\Catalog\Model\Category $category */
foreach ($listCategories as $category) {
$name = 'id_' . $category->getId();
$select = $connection->select()->from($akeneoConnectorTable)
->where('code = ?', $name)
->where('import = ?', 'category');
$result = $connection->query($select);
if ($row = $result->fetch()) {
if ((int) $row['entity_id'] !== (int) $category->getId()) {
$connection->update(
$akeneoConnectorTable,
['entity_id' => $category->getId()],
'entity_id = ' . $row['entity_id']
);
$connection->update(
$tmp_table,
[
'_entity_id' => $category->getId(),
'path' => '1/' . $category->getId()
],
'_entity_id = ' . $row['entity_id']
);
$connection->update(
$tmp_table,
[
'path' => new Expr(
'REPLACE(`path`, "1/' . $row['entity_id'] . '/",
"1/' . $category->getId() . '/")'
)
],
'CONCAT("/", path, "/") like "/1/'.$row['entity_id'].'/%"'
);
}
} else {
$connection->insert(
$akeneoConnectorTable,
[
'import' => 'category',
'code' => $name,
'entity_id' => $category->getId(),
'created_at' => new Expr('now()')
]
);
}
};
}
} `
The code inside Akeneo of the category needs to be "id_...number..." if this number (index) of the Magento category exists.. it will use that one.
I will now extend the Akeneo connector, but before that, I would like to know if there is a trick to do it.
If I already have a category inside Magento with a name like an example "Catalog". And I create a category inside Akeneo with the name "Catalog". It will not use the existing one, it will create a new one. In that case, I will have 2 categories with the name "Catalog".
Is there any option somewhere, or an easy way to tell Akeneo it should use the existing one and not create a new one?
I'm about to extend the method "setStructure" of "Job/Category.php" with an after plugin and change the temporary and akeneo entities tables. But I still didn't check how to do it, if there is an easy way, better.
Thank you guys!