Преглед на файлове

Fix Asteria product status mapping so Magento Enabled stays enabled.

Magento status is a select; converting 1/2 to "Enabled" made (int) status 0 and imported every product as disabled.

Co-authored-by: Cursor <cursoragent@cursor.com>
chengwl преди 1 седмица
родител
ревизия
66b34d943f

+ 24 - 2
app/Services/Asteria/Magento1ProductReader.php

@@ -307,7 +307,7 @@ class Magento1ProductReader
             'cost'                  => $eav['cost'] ?? '',
             'cost'                  => $eav['cost'] ?? '',
             'weight'                => $eav['weight'] ?? '',
             'weight'                => $eav['weight'] ?? '',
             // Magento: 1=Enabled, 2=Disabled. Keep disabled products importable for review matching.
             // Magento: 1=Enabled, 2=Disabled. Keep disabled products importable for review matching.
-            'status'                => ((int) ($eav['status'] ?? 1) === 1) ? 1 : 0,
+            'status'                => $this->mapMagentoStatus($eav['status'] ?? 1),
             'featured'              => 0,
             'featured'              => 0,
             'new'                   => 0,
             'new'                   => 0,
             'guest_checkout'        => 1,
             'guest_checkout'        => 1,
@@ -338,6 +338,26 @@ class Magento1ProductReader
         return $row;
         return $row;
     }
     }
 
 
+    /**
+     * Magento 1 product status: 1=Enabled, 2=Disabled. Bagisto uses 1/0.
+     */
+    private function mapMagentoStatus(mixed $value): int
+    {
+        $enabled = (int) ($this->config['enabled_status'] ?? 1);
+
+        if (is_numeric($value)) {
+            return (int) $value === $enabled ? 1 : 0;
+        }
+
+        $normalized = strtolower(trim((string) $value));
+
+        if (in_array($normalized, ['1', 'enabled', 'enable', 'yes', 'true'], true)) {
+            return 1;
+        }
+
+        return 0;
+    }
+
     /**
     /**
      * @param  array<int, int>  $entityIds
      * @param  array<int, int>  $entityIds
      * @return array<int, array<string, mixed>>
      * @return array<int, array<string, mixed>>
@@ -403,7 +423,9 @@ class Magento1ProductReader
         foreach ($attributes as $code => $info) {
         foreach ($attributes as $code => $info) {
             $input = (string) ($info->frontend_input ?? '');
             $input = (string) ($info->frontend_input ?? '');
 
 
-            if (! in_array($input, ['select', 'multiselect'], true)) {
+            // Magento status is a select (1=Enabled, 2=Disabled). Keep the numeric
+            // value so mapMagentoStatus can map it; converting to "Enabled" makes (int) 0.
+            if ($code === 'status' || ! in_array($input, ['select', 'multiselect'], true)) {
                 continue;
                 continue;
             }
             }
 
 

Файловите разлики са ограничени, защото са твърде много
+ 1 - 1
docs/asteria-migration.md


+ 2 - 0
packages/Webkul/BagistoApi/tests/Unit/Migration/Magento1ProductReaderTest.php

@@ -76,6 +76,7 @@ class Magento1ProductReaderTest extends TestCase
         $this->assertCount(2, $product['options']);
         $this->assertCount(2, $product['options']);
         $this->assertCount(4, $product['variants']);
         $this->assertCount(4, $product['variants']);
         $this->assertSame('WIG-001-1-1', $product['variants'][0]['sku']);
         $this->assertSame('WIG-001-1-1', $product['variants'][0]['sku']);
+        $this->assertSame(1, (int) $product['status']);
         $this->assertSame('12"', $product['variants'][0]['Hair Length']);
         $this->assertSame('12"', $product['variants'][0]['Hair Length']);
         $this->assertSame('12"', $product['variants'][0]['hair_length']);
         $this->assertSame('12"', $product['variants'][0]['hair_length']);
     }
     }
@@ -101,6 +102,7 @@ class Magento1ProductReaderTest extends TestCase
         $page = $reader->fetchProducts(10, 50);
         $page = $reader->fetchProducts(10, 50);
         $this->assertCount(2, $page);
         $this->assertCount(2, $page);
         $this->assertSame(['WIG-002', 'WIG-OFF'], $page->pluck('sku')->all());
         $this->assertSame(['WIG-002', 'WIG-OFF'], $page->pluck('sku')->all());
+        $this->assertSame(1, (int) $page->firstWhere('sku', 'WIG-002')['status']);
         $this->assertSame(0, (int) $page->firstWhere('sku', 'WIG-OFF')['status']);
         $this->assertSame(0, (int) $page->firstWhere('sku', 'WIG-OFF')['status']);
 
 
         $skus = $reader->fetchProducts(0, 50)->pluck('sku')->all();
         $skus = $reader->fetchProducts(0, 50)->pluck('sku')->all();

+ 4 - 0
packages/Webkul/BagistoApi/tests/Unit/Migration/MagentoSchema.php

@@ -699,10 +699,14 @@ class MagentoSchema
         ]);
         ]);
 
 
         $db->table('eav_attribute_option')->insert([
         $db->table('eav_attribute_option')->insert([
+            ['option_id' => 1, 'attribute_id' => self::ATTR_PRODUCT_STATUS, 'sort_order' => 1],
+            ['option_id' => 2, 'attribute_id' => self::ATTR_PRODUCT_STATUS, 'sort_order' => 2],
             ['option_id' => self::HAIR_COLORR_OPTION_ID, 'attribute_id' => self::ATTR_PRODUCT_HAIR_COLORR, 'sort_order' => 1],
             ['option_id' => self::HAIR_COLORR_OPTION_ID, 'attribute_id' => self::ATTR_PRODUCT_HAIR_COLORR, 'sort_order' => 1],
         ]);
         ]);
 
 
         $db->table('eav_attribute_option_value')->insert([
         $db->table('eav_attribute_option_value')->insert([
+            ['option_id' => 1, 'store_id' => 0, 'value' => 'Enabled'],
+            ['option_id' => 2, 'store_id' => 0, 'value' => 'Disabled'],
             ['option_id' => self::HAIR_COLORR_OPTION_ID, 'store_id' => 0, 'value' => 'Natural Black'],
             ['option_id' => self::HAIR_COLORR_OPTION_ID, 'store_id' => 0, 'value' => 'Natural Black'],
         ]);
         ]);
     }
     }

+ 6 - 0
packages/Webkul/BagistoApi/tests/Unit/Migration/MigrateAsteriaProductsCommandTest.php

@@ -89,6 +89,12 @@ class MigrateAsteriaProductsCommandTest extends BagistoApiTestCase
         $this->assertSame(self::MID + 10, (int) $product->migrated_from_asteria_id);
         $this->assertSame(self::MID + 10, (int) $product->migrated_from_asteria_id);
         $this->assertSame((int) $product->id, (int) $product->migrated_from_asteria_id);
         $this->assertSame((int) $product->id, (int) $product->migrated_from_asteria_id);
         $this->assertSame('flexible_variant', $product->type);
         $this->assertSame('flexible_variant', $product->type);
+        $statusAttributeId = DB::table('attributes')->where('code', 'status')->value('id');
+        $this->assertNotNull($statusAttributeId);
+        $this->assertSame(1, (int) DB::table('product_attribute_values')
+            ->where('product_id', $product->id)
+            ->where('attribute_id', $statusAttributeId)
+            ->value('boolean_value'));
 
 
         $this->assertSame('Lace Front Wig', $this->attributeText($product->id, 'name'));
         $this->assertSame('Lace Front Wig', $this->attributeText($product->id, 'name'));
         $this->assertEquals(99.0, (float) $this->attributeFloat($product->id, 'price'));
         $this->assertEquals(99.0, (float) $this->attributeFloat($product->id, 'price'));