Skip to content

Commit

Permalink
Merge pull request #661 from akeneo/release/104.0.3
Browse files Browse the repository at this point in the history
Release/104.0.3
  • Loading branch information
magentix committed Sep 28, 2023
2 parents 7f7d648 + 6b0bf5e commit f8cf0d2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,7 @@
### Version 104.0.2 :
* PGTO-369: Fix product status assignment regression
* PGTO-363: Fix visibility update

### Version 104.0.3 :
* PGTO-378: Fix product without category attribution
* PGTO-380: Rebuild Visual Merchandiser Dynamic Categories
2 changes: 1 addition & 1 deletion Job/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2965,7 +2965,7 @@ public function setCategories(): void
// create data to insert in catalog_product_entity
$notInWhere = [];
foreach ($categoriesByProduct as $row) {
$categoryList = explode(',', $row['categories']);
$categoryList = explode(',', (string)$row['categories']);
foreach ($categoryList as $category) {
$data = [
$row['_entity_id'],
Expand Down
79 changes: 79 additions & 0 deletions Plugin/RebuildDynamicCategories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Akeneo\Connector\Plugin;

use Akeneo\Connector\Job\Product;
use Exception;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\VisualMerchandiser\Model\Category\Builder;
use Magento\VisualMerchandiser\Model\ResourceModel\Rules\Collection as RulesCollection;
use Magento\VisualMerchandiser\Model\ResourceModel\Rules\CollectionFactory as RulesCollectionFactory;

/**
* @author Bartosz Kubicki
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.dnd.fr/
*/
class RebuildDynamicCategories
{
private CategoryResource $categoryResource;
private CategoryCollectionFactory $categoryCollectionFactory;
private ModuleManager $moduleManager;

public function __construct(
CategoryResource $categoryResource,
CategoryCollectionFactory $categoryCollectionFactory,
ModuleManager $moduleManager
) {
$this->categoryResource = $categoryResource;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->moduleManager = $moduleManager;
}

public function afterSetCategories(Product $subject, $result)
{
if ($this->moduleManager->isEnabled('Magento_VisualMerchandiser')) {
$this->rebuildDynamicCategories($subject);
}

return $result;
}

private function rebuildDynamicCategories(Product $productImport): void
{
/** @var $rulesCollection RulesCollection */
$rulesCollection = ObjectManager::getInstance()->get(RulesCollectionFactory::class)->create();
$rulesCollection->addFieldToFilter('is_active', ['eq' => 1]);
$categoriesIds = $rulesCollection->getColumnValues('category_id');

$categoryCollection = $this->categoryCollectionFactory->create();
$categoryCollection->addAttributeToSelect('*')
->addFieldToFilter($this->categoryResource->getEntityIdField(), ['in' => $categoriesIds]);

foreach ($categoryCollection as $category) {
ObjectManager::getInstance()->get(Builder::class)->rebuildCategory($category);
$this->saveRebuiltCategory($productImport, $category);
}
}

private function saveRebuiltCategory(Product $productImport, Category $category): void
{
try {
$this->categoryResource->save($category);
} catch (Exception $exception) {
$productImport->setAdditionalMessage(
__(
'Dynamic category %1 (ID: %2) couldn\'t be rebuilt correctly',
$category->getName(),
$category->getId()
)
);
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"nyholm/psr7": "^1.5"
},
"type": "magento2-module",
"version": "104.0.2",
"version": "104.0.3",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
4 changes: 4 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Akeneo\Connector\Api\LogRepositoryInterface" type="Akeneo\Connector\Model\LogRepository"/>

<type name="Akeneo\Connector\Job\Product">
<plugin name="akeneo_job_product_visual_merchandiser_rebuild" type="Akeneo\Connector\Plugin\RebuildDynamicCategories"/>
</type>

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
Expand Down

0 comments on commit f8cf0d2

Please sign in to comment.