|
|
@@ -81,12 +81,14 @@ class MigrateAsteriaReviews extends Command
|
|
|
}
|
|
|
|
|
|
// ── Build product and customer lookup maps ────────────────────────────────
|
|
|
- $this->info('Building product SKU map…');
|
|
|
+ // Matching ignores Magento/Bagisto product status and inventory — disabled or
|
|
|
+ // out-of-stock products still receive reviews when the SKU (or Asteria id) exists.
|
|
|
+ $this->info('Building product map (status/stock ignored)…');
|
|
|
$productIdMap = $this->buildProductIdMap($connection);
|
|
|
- $this->line(' '.count($productIdMap).' Magento products matched to Bagisto products by SKU.');
|
|
|
+ $this->line(' '.count($productIdMap).' Magento products matched to Bagisto products (SKU / migrated_from_asteria_id).');
|
|
|
|
|
|
if (empty($productIdMap)) {
|
|
|
- $this->warn('No matching products found. Are the SKUs consistent between the two stores?');
|
|
|
+ $this->warn('No matching products found. Import products first (including disabled: ASTERIA_PRODUCTS_ONLY_ENABLED=false).');
|
|
|
}
|
|
|
|
|
|
$this->info('Building customer e-mail map…');
|
|
|
@@ -258,30 +260,61 @@ class MigrateAsteriaReviews extends Command
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Build a map of magento_product_id → bagisto_product_id by joining
|
|
|
- * Magento's catalog_product_entity.sku against Bagisto's products.sku.
|
|
|
+ * Build magento_product_id → bagisto_product_id.
|
|
|
+ *
|
|
|
+ * Prefer products.migrated_from_asteria_id, then fall back to SKU.
|
|
|
+ * Does not filter Magento or Bagisto product status / inventory.
|
|
|
*
|
|
|
* @return array<int, int>
|
|
|
*/
|
|
|
private function buildProductIdMap(string $connection): array
|
|
|
{
|
|
|
- // Fetch all Magento SKUs.
|
|
|
- $magentoPairs = DB::connection($connection)
|
|
|
+ $magentoRows = DB::connection($connection)
|
|
|
->table('catalog_product_entity')
|
|
|
->select('entity_id', 'sku')
|
|
|
- ->get()
|
|
|
- ->pluck('entity_id', 'sku') // sku → magento_id
|
|
|
- ->toArray();
|
|
|
+ ->get();
|
|
|
|
|
|
- if (empty($magentoPairs)) {
|
|
|
+ if ($magentoRows->isEmpty()) {
|
|
|
return [];
|
|
|
}
|
|
|
|
|
|
- // Match against Bagisto's products table.
|
|
|
- $skus = array_keys($magentoPairs);
|
|
|
- $map = [];
|
|
|
+ $map = [];
|
|
|
+
|
|
|
+ // 1) Match by migrated_from_asteria_id (covers disabled / out-of-stock Bagisto rows).
|
|
|
+ if (Schema::hasColumn('products', 'migrated_from_asteria_id')) {
|
|
|
+ $asteriaIds = $magentoRows->pluck('entity_id')->map(fn ($id) => (int) $id)->all();
|
|
|
+
|
|
|
+ foreach (array_chunk($asteriaIds, 500) as $chunk) {
|
|
|
+ $rows = DB::table('products')
|
|
|
+ ->select('id', 'migrated_from_asteria_id')
|
|
|
+ ->whereIn('migrated_from_asteria_id', $chunk)
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ foreach ($rows as $row) {
|
|
|
+ $map[(int) $row->migrated_from_asteria_id] = (int) $row->id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2) Fall back to SKU for Magento products not yet linked by Asteria id.
|
|
|
+ $magentoPairs = [];
|
|
|
+
|
|
|
+ foreach ($magentoRows as $row) {
|
|
|
+ $magentoId = (int) $row->entity_id;
|
|
|
+ $sku = trim((string) $row->sku);
|
|
|
+
|
|
|
+ if ($sku === '' || isset($map[$magentoId])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $magentoPairs[$sku] = $magentoId;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($magentoPairs === []) {
|
|
|
+ return $map;
|
|
|
+ }
|
|
|
|
|
|
- foreach (array_chunk($skus, 500) as $chunk) {
|
|
|
+ foreach (array_chunk(array_keys($magentoPairs), 500) as $chunk) {
|
|
|
$rows = DB::table('products')
|
|
|
->select('id', 'sku')
|
|
|
->whereIn('sku', $chunk)
|
|
|
@@ -290,8 +323,8 @@ class MigrateAsteriaReviews extends Command
|
|
|
foreach ($rows as $row) {
|
|
|
$magentoId = $magentoPairs[$row->sku] ?? null;
|
|
|
|
|
|
- if ($magentoId !== null) {
|
|
|
- $map[(int) $magentoId] = (int) $row->id;
|
|
|
+ if ($magentoId !== null && ! isset($map[$magentoId])) {
|
|
|
+ $map[$magentoId] = (int) $row->id;
|
|
|
}
|
|
|
}
|
|
|
}
|