3
3
namespace Algolia \AlgoliaSearch \Model \Indexer ;
4
4
5
5
use Algolia \AlgoliaSearch \Model \Indexer \Category as CategoryIndexer ;
6
- use Closure ;
7
- use Magento \Catalog \Model \Category as Category ;
6
+ use Magento \Catalog \Model \Category as CategoryModel ;
8
7
use Magento \Catalog \Model \ResourceModel \Category as CategoryResourceModel ;
9
- use Magento \Catalog \Model \ResourceModel \Product \Collection as ProductCollection ;
10
8
use Magento \Framework \Indexer \IndexerRegistry ;
11
9
12
10
class CategoryObserver
@@ -18,53 +16,30 @@ public function __construct(IndexerRegistry $indexerRegistry)
18
16
$ this ->indexer = $ indexerRegistry ->get ('algolia_categories ' );
19
17
}
20
18
21
- /**
22
- * @param CategoryResourceModel $categoryResource
23
- * @param Closure $proceed
24
- * @param Category $category
25
- *
26
- * @return mixed
27
- */
28
- public function aroundSave (
19
+ public function afterSave (
29
20
CategoryResourceModel $ categoryResource ,
30
- Closure $ proceed ,
31
- Category $ category
21
+ $ result ,
22
+ CategoryModel $ category
32
23
) {
33
- $ categoryResource ->addCommitCallback (function () use ($ category ) {
34
- if (!$ this ->indexer ->isScheduled ()) {
35
- /** @var ProductCollection $productCollection */
36
- $ productCollection = $ category ->getProductCollection ();
37
- CategoryIndexer::$ affectedProductIds = (array ) $ productCollection ->getAllIds ();
24
+ if (!$ this ->indexer ->isScheduled ()) {
25
+ /** @var Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
26
+ $ productCollection = $ category ->getProductCollection ();
27
+ CategoryIndexer::$ affectedProductIds = (array ) $ productCollection ->getColumnValues ('entity_id ' );
38
28
39
- $ this ->indexer ->reindexRow ($ category ->getId ());
40
- }
41
- });
42
-
43
- return $ proceed ($ category );
29
+ $ this ->indexer ->reindexRow ($ category ->getId ());
30
+ }
44
31
}
45
32
46
- /**
47
- * @param CategoryResourceModel $categoryResource
48
- * @param Closure $proceed
49
- * @param Category $category
50
- *
51
- * @return mixed
52
- */
53
- public function aroundDelete (
33
+ public function beforeDelete (
54
34
CategoryResourceModel $ categoryResource ,
55
- Closure $ proceed ,
56
- Category $ category
35
+ CategoryModel $ category
57
36
) {
58
- $ categoryResource ->addCommitCallback (function () use ($ category ) {
59
- if (!$ this ->indexer ->isScheduled ()) {
60
- /** @var ProductCollection $productCollection */
61
- $ productCollection = $ category ->getProductCollection ();
62
- CategoryIndexer::$ affectedProductIds = (array ) $ productCollection ->getAllIds ();
63
-
64
- $ this ->indexer ->reindexRow ($ category ->getId ());
65
- }
66
- });
37
+ if (!$ this ->indexer ->isScheduled ()) {
38
+ /* we are using products position because getProductCollection() does use correct store */
39
+ $ productCollection = $ category ->getProductsPosition ();
40
+ CategoryIndexer::$ affectedProductIds = array_keys ($ productCollection );
67
41
68
- return $ proceed ($ category );
42
+ $ this ->indexer ->reindexRow ($ category ->getId ());
43
+ }
69
44
}
70
45
}
0 commit comments