2323use Magento \Framework \Exception \LocalizedException ;
2424use Psr \Log \LoggerInterface ;
2525use Symfony \Component \Console \Output \OutputInterface ;
26+ use \Magento \Catalog \Api \Data \ProductInterface ;
27+ use Magento \Framework \EntityManager \MetadataPool ;
2628
2729/**
2830 * Service class responsible for migrating product positions from Magento legacy catalog table to ElasticSuite table.
4042 * @category Smile
4143 * @package Smile\ElasticsuiteCatalog
4244 * @author Vadym Honcharuk <[email protected] > 45+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4346 */
4447class CategoryPositionMigrator
4548{
@@ -95,6 +98,11 @@ class CategoryPositionMigrator
9598 */
9699 private $ logger ;
97100
101+ /**
102+ * @var MetadataPool
103+ */
104+ private $ metadataPool ;
105+
98106 /**
99107 * Constructor.
100108 *
@@ -103,20 +111,23 @@ class CategoryPositionMigrator
103111 * @param CategoryProductResource $categoryProductResource Magento legacy category-product relation resource model.
104112 * @param Config $eavConfig Eav config.
105113 * @param LoggerInterface $logger PSR-compliant logger for debug and error logging.
114+ * @param MetadataPool $metadataPool Magento entity metadata pool.
106115 */
107116 public function __construct (
108117 ResourceConnection $ resource ,
109118 CategoryCollectionFactory $ categoryCollectionFactory ,
110119 CategoryProductResource $ categoryProductResource ,
111120 Config $ eavConfig ,
112- LoggerInterface $ logger
121+ LoggerInterface $ logger ,
122+ MetadataPool $ metadataPool
113123 ) {
114124 $ this ->resource = $ resource ;
115125 $ this ->connection = $ resource ->getConnection ();
116126 $ this ->categoryCollectionFactory = $ categoryCollectionFactory ;
117127 $ this ->categoryProductResource = $ categoryProductResource ;
118128 $ this ->eavConfig = $ eavConfig ;
119129 $ this ->logger = $ logger ;
130+ $ this ->metadataPool = $ metadataPool ;
120131 }
121132
122133 /**
@@ -264,6 +275,8 @@ public function migrateCategoryPositions(
264275 $ visibilityAttribute = $ this ->eavConfig ->getAttribute (Product::ENTITY , 'visibility ' );
265276 $ visibilityAttributeId = (int ) $ visibilityAttribute ->getAttributeId ();
266277
278+ $ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
279+
267280 $ storeId = 0 ;
268281 $ offset = 0 ;
269282 $ migratedCount = 0 ;
@@ -297,7 +310,7 @@ public function migrateCategoryPositions(
297310 ->from (['ccp ' => $ legacyTable ], ['product_id ' , 'position ' ])
298311 ->joinLeft (
299312 ['cpei ' => $ visibilityTable ],
300- 'cpei.row_id = ccp.product_id '
313+ 'cpei. ' . $ linkField . ' = ccp.product_id '
301314 . ' AND cpei.attribute_id = ' . $ visibilityAttributeId
302315 . ' AND cpei.store_id = ' . $ storeId ,
303316 ['' ]
0 commit comments