Skip to content

Commit 82a5edd

Browse files
committed
Added mass action to bulk enrich existing products
1 parent 4d2536d commit 82a5edd

File tree

10 files changed

+164
-8
lines changed

10 files changed

+164
-8
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\CatalogDataAI\Controller\Adminhtml\Product;
5+
6+
use Magento\Backend\App\Action\Context;
7+
use Magento\Backend\Model\View\Result\Redirect;
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Backend\App\Action;
10+
use Magento\Catalog\Controller\Adminhtml\Product\Builder;
11+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
12+
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\Controller\ResultFactory;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Ui\Component\MassAction\Filter;
17+
use MageOS\CatalogDataAI\Model\Config;
18+
use MageOS\CatalogDataAI\Model\Product\Publisher;
19+
20+
class MassEnrich extends Action implements HttpPostActionInterface
21+
{
22+
protected $overwrite = true;
23+
public function __construct(
24+
Context $context,
25+
private Filter $filter,
26+
private CollectionFactory $collectionFactory,
27+
private Config $config,
28+
private Publisher $publisher,
29+
private ProductRepositoryInterface $productRepository,
30+
) {
31+
parent::__construct($context);
32+
}
33+
34+
/**
35+
* Mass Delete Action
36+
*
37+
* @return Redirect
38+
* @throws LocalizedException
39+
*/
40+
public function execute()
41+
{
42+
$collection = $this->filter->getCollection($this->collectionFactory->create());
43+
44+
$productEnriched = 0;
45+
if($this->config->isEnabled()) {
46+
/** @var \Magento\Catalog\Model\Product $product */
47+
foreach ($collection->getItems() as $product) {
48+
//@TODO: we hit rate limit, change to batching the request
49+
$this->publisher->execute($product->getId(), $this->overwrite);
50+
$productEnriched++;
51+
}
52+
53+
if ($productEnriched) {
54+
$this->messageManager->addSuccessMessage(
55+
__('A total of %1 record(s) are scheduled to get data enriched.', $productEnriched)
56+
);
57+
}
58+
}
59+
else {
60+
$this->messageManager->addErrorMessage(
61+
__('Data enrichment is disabled. Please enable it in the configuration.')
62+
);
63+
}
64+
65+
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('catalog/*/index');
66+
}
67+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\CatalogDataAI\Controller\Adminhtml\Product;
5+
6+
use MageOS\CatalogDataAI\Controller\Adminhtml\Product\MassEnrich;
7+
8+
class MassEnrichSafe extends MassEnrich
9+
{
10+
protected $overwrite = false;
11+
}

Model/Product/Consumer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Magento\Catalog\Model\ProductRepository;
77
use Magento\Store\Model\StoreManagerInterface;
8+
use MageOS\CatalogDataAI\Model\Product\Request;
89
use MageOS\CatalogDataAI\Model\Product\Enricher;
910

1011
/**
@@ -22,11 +23,12 @@ public function __construct(
2223
private StoreManagerInterface $storeManager
2324
) {}
2425

25-
public function execute($id)
26+
public function execute(Request $request)
2627
{
2728
// @TODO: enrich for all stores if different value or language
2829
$this->storeManager->setCurrentStore(0);
29-
$product = $this->productRepository->getById($id);
30+
$product = $this->productRepository->getById($request->getId());
31+
$product->setData('mageos_catalogai_overwrite', $request->getOverwrite());
3032
$this->enricher->execute($product);
3133
$this->productRepository->save($product);
3234
}

Model/Product/Enricher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function parsePrompt($prompt, $product): String
4444

4545
public function enrichAttribute($product, $attributeCode)
4646
{
47-
if($product->getData($attributeCode)) {
47+
if(!$product->getData('mageos_catalogai_overwrite') && $product->getData($attributeCode)){
4848
return;
4949
}
5050
if($prompt = $this->config->getProductPrompt($attributeCode)) {

Model/Product/Publisher.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MageOS\CatalogDataAI\Model\Product;
55
use Magento\Framework\MessageQueue\PublisherInterface;
6+
use MageOS\CatalogDataAI\Model\Product\RequestFactory;
67

78
class Publisher
89
{
@@ -14,14 +15,19 @@ class Publisher
1415
*/
1516
public function __construct
1617
(
17-
private PublisherInterface $publisher
18+
private PublisherInterface $publisher,
19+
private RequestFactory $requestFactory,
1820
) {}
1921

2022
/**
2123
* @param data
2224
*/
23-
public function execute(int|string $productId)
25+
public function execute(int|string $productId, $overwrite = false)
2426
{
25-
$this->publisher->publish(self::TOPIC_NAME, (int)$productId);
27+
$request = $this->requestFactory->create([
28+
'id' => (int)$productId,
29+
'overwrite' => $overwrite
30+
]);
31+
$this->publisher->publish(self::TOPIC_NAME, $request);
2632
}
2733
}

Model/Product/Request.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\CatalogDataAI\Model\Product;
5+
6+
/**
7+
* Data model for enrichment message queue.
8+
*/
9+
class Request
10+
{
11+
public function __construct(
12+
private int $id,
13+
private bool $overwrite
14+
) {}
15+
16+
/**
17+
* Retrieve products id.
18+
* @return int
19+
*/
20+
public function getId(): int
21+
{
22+
return $this->id;
23+
}
24+
25+
/**
26+
* Retrieve overwrite flag.
27+
* @return bool
28+
*/
29+
public function getOverwrite(): bool
30+
{
31+
return $this->overwrite;
32+
}
33+
}

Observer/Product/SaveAfter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function execute(Observer $observer): void
2121
$product = $observer->getProduct();
2222

2323
if($this->config->canEnrich($product) && $this->config->isAsync()) {
24-
$this->publisher->execute($product->getId());
24+
$this->publisher->execute($product->getId(), false);
2525
}
2626
}
2727
}

etc/adminhtml/routes.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
3+
<router id="admin">
4+
<route id="catalogai" frontName="catalogai">
5+
<module name="MageOS_CatalogDataAI" before="Magento_Backend" />
6+
</route>
7+
</router>
8+
</config>

etc/communication.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Communication/etc/communication.xsd">
3-
<topic name="mageos.product.enrich" request="int"/>
3+
<topic name="mageos.product.enrich" request="MageOS\CatalogDataAI\Model\Product\Request"/>
44
</config>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
3+
<listingToolbar name="listing_top">
4+
<massaction name="listing_massaction">
5+
<action name="enrich">
6+
<settings>
7+
<confirm>
8+
<message translate="true">Enrich selected items? This may overwrite your previously saved product data. If you wish to enrich only the missing values, use `Enrich(safe)`.</message>
9+
<title translate="true">Enrich product data</title>
10+
</confirm>
11+
<url path="catalogai/product/massEnrich"/>
12+
<type>enrich</type>
13+
<label translate="true">AI Enrich</label>
14+
</settings>
15+
</action>
16+
<action name="enrich_safe">
17+
<settings>
18+
<confirm>
19+
<message translate="true">Enrich selected items? This would fill only missing values with AI, your previously created content will not be touched.</message>
20+
<title translate="true">Enrich product data (Safe)</title>
21+
</confirm>
22+
<url path="catalogai/product/massEnrichSafe"/>
23+
<type>safe_enrich</type>
24+
<label translate="true">AI Enrich(Safe)</label>
25+
</settings>
26+
</action>
27+
</massaction>
28+
</listingToolbar>
29+
</listing>

0 commit comments

Comments
 (0)