Skip to content

Commit 84b5375

Browse files
committed
Re-add upsell_ids as backwards compatible accessor + trait for product
links
1 parent ecd8b79 commit 84b5375

4 files changed

Lines changed: 79 additions & 36 deletions

File tree

src/Models/Product.php

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Rapidez\Core\Models\Traits\HasAlternatesThroughRewrites;
1919
use Rapidez\Core\Models\Traits\HasCustomAttributes;
2020
use Rapidez\Core\Models\Traits\Product\BackwardsCompatibleAccessors;
21+
use Rapidez\Core\Models\Traits\Product\HasProductLinks;
2122
use Rapidez\Core\Models\Traits\Product\HasSuperAttributes;
2223
use Rapidez\Core\Models\Traits\Product\Searchable;
2324
use Rapidez\Core\Models\Traits\UsesCallbackRelations;
@@ -27,6 +28,7 @@ class Product extends Model
2728
use BackwardsCompatibleAccessors;
2829
use HasAlternatesThroughRewrites;
2930
use HasCustomAttributes;
31+
use HasProductLinks;
3032
use HasSuperAttributes;
3133
use Searchable;
3234
use UsesCallbackRelations;
@@ -152,40 +154,6 @@ protected function grouped(): Attribute
152154
return Attribute::get(fn () => $this->children);
153155
}
154156

155-
public function productLinks(): HasMany
156-
{
157-
return $this->hasMany(
158-
config('rapidez.models.product_link', ProductLink::class),
159-
'product_id', 'entity_id',
160-
);
161-
}
162-
163-
public function productLinkParents(): HasMany
164-
{
165-
return $this->hasMany(
166-
config('rapidez.models.product_link', ProductLink::class),
167-
'linked_product_id', 'entity_id',
168-
);
169-
}
170-
171-
public function getLinkedProducts(string $type): Collection
172-
{
173-
return $this->productLinks()
174-
->with('linkedProduct')
175-
->where('code', $type)
176-
->get()
177-
->pluck('linkedProduct');
178-
}
179-
180-
public function getLinkedParents(string $type): Collection
181-
{
182-
return $this->productLinkParents()
183-
->with('linkedParent')
184-
->where('code', $type)
185-
->get()
186-
->pluck('linkedParent');
187-
}
188-
189157
public function categoryProducts(): HasMany
190158
{
191159
return $this

src/Models/Traits/Product/BackwardsCompatibleAccessors.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,24 @@ protected function inStock(): Attribute
3737
{
3838
return Attribute::get(fn () => $this->stock->is_in_stock);
3939
}
40+
41+
/**
42+
* @deprecated please use $product->upsells()->pluck('linked_product_id')
43+
*/
44+
public function upsellIds(): Attribute
45+
{
46+
return Attribute::get(fn () =>
47+
$this->upsells()->pluck('linked_product_id')
48+
);
49+
}
50+
51+
/**
52+
* @deprecated please use $product->relationProducts()->pluck('linked_product_id')
53+
*/
54+
public function relationIds(): Attribute
55+
{
56+
return Attribute::get(fn () =>
57+
$this->relationProducts()->pluck('linked_product_id')
58+
);
59+
}
4060
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Rapidez\Core\Models\Traits\Product;
4+
5+
use Illuminate\Database\Eloquent\Relations\HasMany;
6+
use Rapidez\Core\Models\ProductLink;
7+
8+
trait HasProductLinks
9+
{
10+
public function productLinks(): HasMany
11+
{
12+
return $this->hasMany(
13+
config('rapidez.models.product_link', ProductLink::class),
14+
'product_id', 'entity_id',
15+
);
16+
}
17+
18+
public function productLinkParents(): HasMany
19+
{
20+
return $this->hasMany(
21+
config('rapidez.models.product_link', ProductLink::class),
22+
'linked_product_id', 'entity_id',
23+
);
24+
}
25+
26+
public function linkedProducts(string $type): HasMany
27+
{
28+
return $this->productLinks()->where('code', $type);
29+
}
30+
31+
public function linkedParents(string $type): HasMany
32+
{
33+
return $this->productLinkParents()->where('code', $type);
34+
}
35+
36+
public function relationProducts(): HasMany
37+
{
38+
return $this->linkedProducts('relation');
39+
}
40+
41+
public function superProducts(): HasMany
42+
{
43+
return $this->linkedProducts('super');
44+
}
45+
46+
public function upsells(): HasMany
47+
{
48+
return $this->linkedProducts('up_sell');
49+
}
50+
51+
public function crosssells(): HasMany
52+
{
53+
return $this->linkedProducts('cross_sell');
54+
}
55+
}

tests/Unit/ProductTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public function product_can_have_upsells_and_crosssells()
5656
{
5757
$product = Product::find(1578);
5858

59-
$this->assertEquals(8, $product->getLinkedProducts('up_sell')->count(), 'Product 1578 does not have 8 upsells.');
60-
$this->assertEquals(4, $product->getLinkedProducts('cross_sell')->count(), 'Product 1578 does not have 4 crosssells.');
59+
$this->assertEquals(8, $product->linkedProducts('up_sell')->count(), 'Product 1578 does not have 8 upsells.');
60+
$this->assertEquals(4, $product->linkedProducts('cross_sell')->count(), 'Product 1578 does not have 4 crosssells.');
6161
}
6262

6363
#[Test]

0 commit comments

Comments
 (0)