Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.

Commit be88172

Browse files
committed
chore: 🚧 Resolve the product price with associated price tiers
1 parent 5036d07 commit be88172

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

src/Database/Factories/MagentoTierPriceFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function definition()
2323
{
2424
return [
2525
'magento_product_id' => MagentoProductFactory::new(),
26+
'customer_group_id' => MagentoCustomerGroupFactory::new(),
2627
'value' => $this->faker->randomFloat(2, 1, 500),
2728
'quantity' => $this->faker->numberBetween(1, 100),
2829
'extension_attributes' => [],

src/Database/Migrations/2021_05_20_211536_create_magento_tier_prices_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function up()
1111
Schema::create('magento_tier_prices', function (Blueprint $table) {
1212
$table->id();
1313
$table->bigInteger('magento_product_id')->index();
14+
$table->bigInteger('customer_group_id')->index();
1415
$table->decimal('value', 15, 4)->default(0.00);
1516
$table->integer('quantity')->default(0);
1617
$table->text('extension_attributes')->nullable();

src/Models/MagentoProduct.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Support\Carbon;
7+
use Illuminate\Support\Facades\Auth;
78
use Illuminate\Support\Facades\Storage;
89

910
class MagentoProduct extends Model
@@ -183,6 +184,26 @@ public function salePrice()
183184
: null;
184185
}
185186

187+
/**
188+
* Resolve the price based on the tier pricing.
189+
*
190+
* @return float
191+
*/
192+
public function resolvePrice()
193+
{
194+
if (! Auth::check() || ! Auth::user() instanceof MagentoCustomer || ! $this->tierPrices) {
195+
return $this->price;
196+
}
197+
198+
$tierPrice = $this->tierPrices
199+
->filter(fn ($tier) => Auth::user()->group_id == $tier->customer_group_id)
200+
->first();
201+
202+
return $tierPrice
203+
? $tierPrice->value
204+
: $this->price;
205+
}
206+
186207
/**
187208
* The Magento Product can have many tier prices.
188209
*

src/Models/MagentoTierPrice.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@ public function product()
3939
{
4040
return $this->belongsTo(MagentoProduct::class, 'magento_product_id');
4141
}
42+
43+
/**
44+
* The Magento Customer Group ID belongs to the Magento Customer Group.
45+
*
46+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
47+
*/
48+
public function group()
49+
{
50+
return $this->belongsTo(MagentoCustomerGroup::class, 'customer_group_id');
51+
}
4252
}

tests/Models/MagentoProductModelTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Grayloon\MagentoStorage\Database\Factories\MagentoConfigurableProductOptionFactory;
88
use Grayloon\MagentoStorage\Database\Factories\MagentoCustomAttributeFactory;
99
use Grayloon\MagentoStorage\Database\Factories\MagentoCustomAttributeTypeFactory;
10+
use Grayloon\MagentoStorage\Database\Factories\MagentoCustomerFactory;
1011
use Grayloon\MagentoStorage\Database\Factories\MagentoProductCategoryFactory;
1112
use Grayloon\MagentoStorage\Database\Factories\MagentoProductFactory;
1213
use Grayloon\MagentoStorage\Database\Factories\MagentoProductLinkFactory;
@@ -403,4 +404,45 @@ public function test_magento_product_can_have_many_price_tiers()
403404
$this->assertNotEmpty($response);
404405
$this->assertEquals(5, $response->count());
405406
}
407+
408+
/** @test */
409+
public function it_returns_tier_price_on_resolve_price()
410+
{
411+
$product = MagentoProductFactory::new()->create();
412+
$tierPrice = MagentoTierPriceFactory::new()->create([
413+
'magento_product_id' => $product->id,
414+
'value' => 10.99,
415+
]);
416+
$customer = MagentoCustomerFactory::new()->create([
417+
'group_id' => $tierPrice->customer_group_id,
418+
]);
419+
420+
$this->actingAs($customer);
421+
422+
$product->load('tierPrices');
423+
$this->assertEquals(10.99, $product->resolvePrice());
424+
}
425+
426+
/** @test */
427+
public function it_returns_price_on_resolve_price()
428+
{
429+
$product = MagentoProductFactory::new()->create([
430+
'price' => 50.99,
431+
]);
432+
$product->load('tierPrices');
433+
$this->assertEquals(50.99, $product->resolvePrice());
434+
435+
MagentoTierPriceFactory::new()->create([
436+
'magento_product_id' => $product->id,
437+
'value' => 10.99,
438+
]);
439+
440+
$product->load('tierPrices');
441+
$this->assertEquals(50.99, $product->resolvePrice());
442+
443+
$customer = MagentoCustomerFactory::new()->create();
444+
$this->actingAs($customer);
445+
$product->load('tierPrices');
446+
$this->assertEquals(50.99, $product->resolvePrice());
447+
}
406448
}

tests/Models/MagentoTierPriceModelTest.php

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

55
use Grayloon\MagentoStorage\Database\Factories\MagentoTierPriceFactory;
6+
use Grayloon\MagentoStorage\Models\MagentoCustomerGroup;
67
use Grayloon\MagentoStorage\Models\MagentoProduct;
78

89
class MagentoTierPriceModelTest extends TestCase
@@ -21,4 +22,13 @@ public function magento_product_id_belongs_to_magento_product()
2122

2223
$this->assertInstanceOf(MagentoProduct::class, $priceTier->product);
2324
}
25+
26+
/** @test */
27+
public function customer_group_id_belongs_to_magento_customer_group()
28+
{
29+
$priceTier = MagentoTierPriceFactory::new()->create();
30+
$priceTier->load('group');
31+
32+
$this->assertInstanceOf(MagentoCustomerGroup::class, $priceTier->group);
33+
}
2434
}

0 commit comments

Comments
 (0)