2
2
3
3
namespace Algolia \AlgoliaSearch \Model \Indexer ;
4
4
5
+ use Algolia \AlgoliaSearch \Exception \DiagnosticsException ;
5
6
use Algolia \AlgoliaSearch \Model \Indexer \Category as CategoryIndexer ;
6
7
use Algolia \AlgoliaSearch \Service \AlgoliaCredentialsManager ;
8
+ use Algolia \AlgoliaSearch \Service \Product \BatchQueueProcessor as ProductBatchQueueProcessor ;
7
9
use Magento \Catalog \Model \Category as CategoryModel ;
8
10
use Magento \Catalog \Model \ResourceModel \Category as CategoryResourceModel ;
9
11
use Magento \Catalog \Model \ResourceModel \Product \Collection as ProductCollection ;
10
12
use Magento \Framework \App \ResourceConnection ;
13
+ use Magento \Framework \Exception \NoSuchEntityException ;
11
14
use Magento \Framework \Indexer \IndexerRegistry ;
15
+ use Magento \Store \Model \StoreManagerInterface ;
12
16
13
17
class CategoryObserver
14
18
{
@@ -17,7 +21,9 @@ class CategoryObserver
17
21
18
22
public function __construct (
19
23
IndexerRegistry $ indexerRegistry ,
24
+ protected StoreManagerInterface $ storeManager ,
20
25
protected ResourceConnection $ resource ,
26
+ protected ProductBatchQueueProcessor $ productBatchQueueProcessor ,
21
27
protected AlgoliaCredentialsManager $ algoliaCredentialsManager
22
28
) {
23
29
$ this ->indexer = $ indexerRegistry ->get ('algolia_categories ' );
@@ -52,8 +58,8 @@ public function afterSave(
52
58
$ changedProductIds = ($ category ->getChangedProductIds () !== null ? (array ) $ category ->getChangedProductIds () : []);
53
59
54
60
if (!$ this ->indexer ->isScheduled ()) {
55
- CategoryIndexer::$ affectedProductIds = array_unique (array_merge ($ changedProductIds , $ collectionIds ));
56
61
$ this ->indexer ->reindexRow ($ category ->getId ());
62
+ $ this ->reindexAffectedProducts (array_unique (array_merge ($ changedProductIds , $ collectionIds )));
57
63
} else {
58
64
// missing logic, if scheduled, when category is saved w/out product, products need to be added to _cl
59
65
if (count ($ changedProductIds ) === 0 && count ($ collectionIds ) > 0 ) {
@@ -82,15 +88,29 @@ public function afterDelete(
82
88
if (!$ this ->indexer ->isScheduled ()) {
83
89
/* we are using products position because getProductCollection() doesn't use correct store */
84
90
$ productCollection = $ category ->getProductsPosition ();
85
- CategoryIndexer::$ affectedProductIds = array_keys ($ productCollection );
86
-
87
91
$ this ->indexer ->reindexRow ($ category ->getId ());
92
+ $ this ->reindexAffectedProducts (array_keys ($ productCollection ));
88
93
}
89
94
});
90
95
91
96
return $ result ;
92
97
}
93
98
99
+ /**
100
+ * @param array $affectedProductIds
101
+ * @return void
102
+ * @throws DiagnosticsException
103
+ * @throws NoSuchEntityException
104
+ */
105
+ protected function reindexAffectedProducts (array $ affectedProductIds ): void
106
+ {
107
+ if (count ($ affectedProductIds ) > 0 ) {
108
+ foreach (array_keys ($ this ->storeManager ->getStores ()) as $ storeId ) {
109
+ $ this ->productBatchQueueProcessor ->processBatch ($ storeId , $ affectedProductIds );
110
+ }
111
+ }
112
+ }
113
+
94
114
/**
95
115
* @param array $productIds
96
116
*/
0 commit comments