Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a5b08dd

Browse files
committed
feat: ability to store product slugs from the custom attribute
1 parent 1434f8c commit a5b08dd

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Database/Factories/MagentoProductFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
'status' => 1,
1515
'visibility' => 1,
1616
'type' => 'simple',
17+
'slug' => $faker->slug,
1718
];
1819
});

src/Support/MagentoProducts.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ protected function syncCustomAttributes($attributes, $product)
107107
$this->syncProductCategories($attribute['value'], $product);
108108
}
109109

110+
if ($attribute['attribute_code'] === 'url_key') {
111+
$product->update([
112+
'slug' => $attribute['value'],
113+
]);
114+
}
115+
110116
if ($this->isImageType($attribute['attribute_code'])) {
111117
$this->downloadImage($attribute['value']);
112118
}

tests/Support/MagentoProductsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,41 @@ public function test_magento_product_download_image_is_correctly_constructed()
181181
Queue::assertPushed(fn (DownloadMagentoProductImage $downloadJob) => $downloadJob->directory === '/pub/media/catalog/product');
182182
Queue::assertPushed(fn (DownloadMagentoProductImage $downloadJob) => $downloadJob->fullUrl === '/pub/media/catalog/product/foo.jpg');
183183
}
184+
185+
public function test_magento_product_applies_slug_from_url_key()
186+
{
187+
$category = factory(MagentoCategory::class)->create();
188+
189+
$products = [
190+
[
191+
'id' => '1',
192+
'name' => 'Dunder Mifflin Paper',
193+
'sku' => 'DFPC001',
194+
'price' => 19.99,
195+
'status' => '1',
196+
'visibility' => '1',
197+
'type_id' => 'simple',
198+
'created_at' => now(),
199+
'updated_at' => now(),
200+
'weight' => 10.00,
201+
'extension_attributes' => [
202+
'website_id' => [1],
203+
],
204+
'custom_attributes' => [
205+
[
206+
'attribute_code' => 'url_key',
207+
'value' => 'dunder-mifflin-paper',
208+
],
209+
],
210+
],
211+
];
212+
213+
$magentoProducts = new MagentoProducts();
214+
215+
$magentoProducts->updateProducts($products);
216+
217+
$product = MagentoProduct::first();
218+
$this->assertNotNull($product);
219+
$this->assertEquals('dunder-mifflin-paper', $product->slug);
220+
}
184221
}

0 commit comments

Comments
 (0)