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

Commit f9156ac

Browse files
committed
chore: ability to save assigned categories to products
1 parent f2e1bd3 commit f9156ac

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

src/Models/MagentoCategory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ public function customAttributes()
3232
{
3333
return $this->morphMany(MagentoCustomAttribute::class, 'attributable');
3434
}
35+
36+
/**
37+
* The products assigned to the category.
38+
*
39+
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
40+
*/
41+
public function products()
42+
{
43+
return $this->hasManyThrough(MagentoProduct::class, MagentoProductCategory::class, 'magento_category_id', 'id', 'id', 'magento_product_id');
44+
}
3545
}

src/Support/MagentoProducts.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Grayloon\Magento\Models\MagentoExtensionAttribute;
77
use Grayloon\Magento\Models\MagentoExtensionAttributeType;
88
use Grayloon\Magento\Models\MagentoProduct;
9+
use Grayloon\Magento\Models\MagentoProductCategory;
910

1011
class MagentoProducts extends PaginatableMagentoService
1112
{
@@ -101,6 +102,10 @@ protected function syncExtensionAttributes($attributes, $product)
101102
protected function syncCustomAttributes($attributes, $product)
102103
{
103104
foreach ($attributes as $attribute) {
105+
if ($attribute['attribute_code'] === 'category_ids') {
106+
$this->syncProductCategories($attribute['value'], $product);
107+
}
108+
104109
if (is_array($attribute['value'])) {
105110
$attribute['value'] = json_encode($attribute['value']);
106111
}
@@ -112,4 +117,27 @@ protected function syncCustomAttributes($attributes, $product)
112117

113118
return $this;
114119
}
120+
121+
/**
122+
* Assign the Product Category IDs that belong to the product.
123+
*
124+
* @param array $categoryIds
125+
* @param \Grayloon\Magento\Models\MagentoProduct\ $product
126+
* @return void
127+
*/
128+
protected function syncProductCategories($categoryIds, $product)
129+
{
130+
if (! $categoryIds) {
131+
return;
132+
}
133+
134+
foreach ($categoryIds as $categoryId) {
135+
MagentoProductCategory::updateOrCreate([
136+
'magento_product_id' => $product->id,
137+
'magento_category_id' => $categoryId,
138+
]);
139+
}
140+
141+
return $this;
142+
}
115143
}

tests/Models/MagentoCategoryModelTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Grayloon\Magento\Tests;
44

55
use Grayloon\Magento\Models\MagentoCategory;
6+
use Grayloon\Magento\Models\MagentoProduct;
7+
use Grayloon\Magento\Models\MagentoProductCategory;
68

79
class MagentoCategoryModelTest extends TestCase
810
{
@@ -39,4 +41,20 @@ public function test_can_add_custom_attributes_to_magento_category()
3941
$this->assertEquals(MagentoCategory::class, $attribute->attributable_type);
4042
$this->assertEquals($category->id, $attribute->attributable_id);
4143
}
44+
45+
public function test_magento_category_can_get_single_product()
46+
{
47+
factory(MagentoCategory::class)->create(); // create non-assigned category.
48+
factory(MagentoProduct::class)->create(); // create non-assigned category.
49+
50+
$category = factory(MagentoCategory::class)->create();
51+
factory(MagentoProductCategory::class)->create([
52+
'magento_category_id' => $category->id,
53+
]);
54+
55+
$products = $category->products()->get();
56+
$this->assertNotEmpty($products);
57+
$this->assertEquals(1, $products->count());
58+
$this->assertInstanceOf(MagentoProduct::class, $products->first());
59+
}
4260
}

tests/Support/MagentoProductsTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Grayloon\Magento\Tests\Support;
44

5+
use Grayloon\Magento\Models\MagentoCategory;
56
use Grayloon\Magento\Models\MagentoProduct;
67
use Grayloon\Magento\Support\MagentoProducts;
78
use Grayloon\Magento\Tests\TestCase;
@@ -24,8 +25,10 @@ public function test_can_count_magento_products()
2425
$this->assertEquals(1, $count);
2526
}
2627

27-
public function test_can_create_magento_product()
28+
public function test_magento_product_adds_associated_category()
2829
{
30+
$category = factory(MagentoCategory::class)->create();
31+
2932
$products = [
3033
[
3134
'id' => '1',
@@ -43,8 +46,8 @@ public function test_can_create_magento_product()
4346
],
4447
'custom_attributes' => [
4548
[
46-
'attribute_code' => 'salesman',
47-
'value' => 'Dwight Schrute',
49+
'attribute_code' => 'category_ids',
50+
'value' => ["1"]
4851
],
4952
],
5053
],
@@ -55,14 +58,11 @@ public function test_can_create_magento_product()
5558
$magentoProducts->updateProducts($products);
5659

5760
$product = MagentoProduct::first();
61+
$productCategories = $product->categories()->get();
5862

5963
$this->assertNotEmpty($product);
60-
$this->assertEquals('Dunder Mifflin Paper', $product->name);
61-
$this->assertNotEmpty($product->extensionAttributes());
62-
$this->assertEquals([1], $product->extensionAttributes()->first()->attribute);
63-
$this->assertEquals('website_id', $product->extensionAttributes()->first()->Type()->first()->type);
64-
$this->assertNotEmpty($product->customAttributes()->get());
65-
$this->assertEquals('Dwight Schrute', $product->customAttributes()->first()->value);
66-
$this->assertEquals('salesman', $product->customAttributes()->first()->attribute_type);
64+
$this->assertNotEmpty($productCategories);
65+
$this->assertCount(1, $productCategories);
66+
$this->assertSame($category->id, $productCategories->first()->id);
6767
}
6868
}

0 commit comments

Comments
 (0)