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

Commit 5f9e29e

Browse files
authored
Merge pull request #13 from grayloon/product-slugs
Store/Update Product Slugs
2 parents 8c0c14f + a5b08dd commit 5f9e29e

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-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
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddSlugToMagentoProducts extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('magento_products', function (Blueprint $table) {
17+
$table->string('slug')->nullable()->index();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('magento_products', function (Blueprint $table) {
29+
$table->removeColumn('slug');
30+
});
31+
}
32+
}

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)