77
88use Magento \Catalog \Model \Indexer \Product \Flat \FlatTableBuilder ;
99use Magento \Catalog \Model \Indexer \Product \Flat \TableBuilder ;
10+ use Magento \Framework \EntityManager \MetadataPool ;
11+ use Magento \Catalog \Api \Data \ProductInterface ;
1012
1113/**
1214 * Class Row reindex action
@@ -22,6 +24,10 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
2224 * @var Eraser
2325 */
2426 protected $ flatItemEraser ;
27+ /**
28+ * @var MetadataPool
29+ */
30+ private $ metadataPool ;
2531
2632 /**
2733 * @param \Magento\Framework\App\ResourceConnection $resource
@@ -32,6 +38,7 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
3238 * @param FlatTableBuilder $flatTableBuilder
3339 * @param Indexer $flatItemWriter
3440 * @param Eraser $flatItemEraser
41+ * @param MetadataPool $metadataPool
3542 */
3643 public function __construct (
3744 \Magento \Framework \App \ResourceConnection $ resource ,
@@ -41,7 +48,8 @@ public function __construct(
4148 TableBuilder $ tableBuilder ,
4249 FlatTableBuilder $ flatTableBuilder ,
4350 Indexer $ flatItemWriter ,
44- Eraser $ flatItemEraser
51+ Eraser $ flatItemEraser ,
52+ MetadataPool $ metadataPool
4553 ) {
4654 parent ::__construct (
4755 $ resource ,
@@ -53,6 +61,7 @@ public function __construct(
5361 );
5462 $ this ->flatItemWriter = $ flatItemWriter ;
5563 $ this ->flatItemEraser = $ flatItemEraser ;
64+ $ this ->metadataPool = $ metadataPool ;
5665 }
5766
5867 /**
@@ -75,18 +84,45 @@ public function execute($id = null)
7584 if ($ tableExists ) {
7685 $ this ->flatItemEraser ->removeDeletedProducts ($ ids , $ store ->getId ());
7786 }
78- if (isset ($ ids [0 ])) {
79- if (!$ tableExists ) {
80- $ this ->_flatTableBuilder ->build (
81- $ store ->getId (),
82- [$ ids [0 ]],
83- $ this ->_valueFieldSuffix ,
84- $ this ->_tableDropSuffix ,
85- false
86- );
87+
88+ /* @var $status \Magento\Eav\Model\Entity\Attribute */
89+ $ status = $ this ->_productIndexerHelper ->getAttribute ('status ' );
90+ $ statusTable = $ status ->getBackendTable ();
91+ $ statusConditions = [
92+ 'store_id IN(0, ' . (int )$ store ->getId () . ') ' ,
93+ 'attribute_id = ' . (int )$ status ->getId (),
94+ 'entity_id = ' . (int )$ id
95+ ];
96+ $ select = $ this ->_connection ->select ();
97+ $ select ->from (
98+ $ statusTable ,
99+ ['value ' ]
100+ )->where (
101+ implode (' AND ' , $ statusConditions )
102+ )->order (
103+ 'store_id DESC '
104+ );
105+ $ result = $ this ->_connection ->query ($ select );
106+ $ status = $ result ->fetch (1 );
107+
108+ if ($ status ['value ' ] == \Magento \Catalog \Model \Product \Attribute \Source \Status::STATUS_ENABLED ) {
109+ if (isset ($ ids [0 ])) {
110+ if (!$ tableExists ) {
111+ $ this ->_flatTableBuilder ->build (
112+ $ store ->getId (),
113+ [$ ids [0 ]],
114+ $ this ->_valueFieldSuffix ,
115+ $ this ->_tableDropSuffix ,
116+ false
117+ );
118+ }
119+ $ this ->flatItemWriter ->write ($ store ->getId (), $ ids [0 ], $ this ->_valueFieldSuffix );
87120 }
88- $ this ->flatItemWriter ->write ($ store ->getId (), $ ids [0 ], $ this ->_valueFieldSuffix );
121+ } else {
122+ $ this ->flatItemEraser ->deleteProductsFromStore ($ id , $ store ->getId ());
89123 }
124+
125+
90126 }
91127 return $ this ;
92128 }
0 commit comments