Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix line fetching in order creation #1956

Open
wants to merge 4 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/core/src/Pipelines/Order/Creation/CreateOrderLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Lunar\Models\Order;
use Lunar\Models\OrderLine;
use Lunar\Utils\Arr;

class CreateOrderLines
{
Expand All @@ -23,8 +24,13 @@ public function handle(Order $order, Closure $next)

foreach ($cart->lines as $cartLine) {
$orderLine = $order->lines->first(function ($line) use ($cartLine) {
return $line->purchasable_id == $cartLine->purchasable_id &&
$line->purchasable_type == $cartLine->purchasable_type;
$diff = Arr::diff($line->meta, $cartLine->meta);

return empty($diff->new) &&
empty($diff->edited) &&
empty($diff->removed) &&
$line->purchasable_type == $cartLine->purchasable_type &&
$line->purchasable_id == $cartLine->purchasable_id;
}) ?: new OrderLine;

$orderLine->fill([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Lunar\Models\Order;
use Lunar\Utils\Arr;

class MapDiscountBreakdown
{
Expand All @@ -18,7 +19,12 @@ public function handle(Order $order, Closure $next)

foreach ($order->lines as $orderLine) {
$cartLine = $cart->lines->first(function ($cartLine) use ($orderLine) {
return $cartLine->purchasable_type == $orderLine->purchasable_type &&
$diff = Arr::diff($cartLine->meta, $orderLine->meta);

return empty($diff->new) &&
empty($diff->edited) &&
empty($diff->removed) &&
$cartLine->purchasable_type == $orderLine->purchasable_type &&
$cartLine->purchasable_id == $orderLine->purchasable_id;
});

Expand Down
125 changes: 125 additions & 0 deletions tests/core/Unit/Pipelines/Order/Creation/MapDiscountBreakdownTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

uses(\Lunar\Tests\Core\TestCase::class);

use Lunar\DiscountTypes\AmountOff;
use Lunar\Models\Cart;
use Lunar\Models\Channel;
use Lunar\Models\Currency;
use Lunar\Models\CustomerGroup;
use Lunar\Models\Discount;
use Lunar\Models\Order;
use Lunar\Models\Price;
use Lunar\Models\ProductVariant;
use Lunar\Pipelines\Order\Creation\CreateOrderLines;
use Lunar\Pipelines\Order\Creation\MapDiscountBreakdown;

uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);

beforeEach(function () {
Currency::factory()->create([
'code' => 'GBP',
'decimal_places' => 2,
]);

Channel::factory()->create([
'default' => true,
]);

CustomerGroup::factory()->create([
'default' => true,
]);
});

test('can map discount with same purchasable with different meta', function () {
$customerGroup = CustomerGroup::getDefault();

$channel = Channel::getDefault();

$currency = Currency::getDefault();

$purchasable = ProductVariant::factory()->create();

Price::factory()->create([
'price' => 100,
'min_quantity' => 1,
'currency_id' => $currency->id,
'priceable_type' => $purchasable->getMorphClass(),
'priceable_id' => $purchasable->id,
]);

$discount = Discount::factory()->create([
'type' => AmountOff::class,
'name' => 'Test Coupon',
'coupon' => '10OFF',
'data' => [
'fixed_value' => false,
'percentage' => 10,
],
]);

$discount->customerGroups()->sync([
$customerGroup->id => [
'enabled' => true,
'starts_at' => now(),
],
]);

$discount->channels()->sync([
$channel->id => [
'enabled' => true,
'starts_at' => now()->subHour(),
],
]);

$cart = Cart::factory()->create([
'channel_id' => $channel->id,
'currency_id' => $currency->id,
'coupon_code' => '10OFF',
]);

$cart->lines()->create([
'purchasable_type' => $purchasable->getMorphClass(),
'purchasable_id' => $purchasable->id,
'quantity' => 1,
'meta' => [
'personalization' => 'Love you mum xxx',
],
]);

$cart->lines()->create([
'purchasable_type' => $purchasable->getMorphClass(),
'purchasable_id' => $purchasable->id,
'quantity' => 1,
'meta' => [
'personalization' => 'Get well soon',
],
]);

$order = Order::factory()->create([
'cart_id' => $cart->id,
]);

$cart->calculate();

app(CreateOrderLines::class)->handle($order, function ($order) {
return $order;
});

app(MapDiscountBreakdown::class)->handle($order, function ($order) {
return $order;
});

$appliedDiscount = $order->discount_breakdown->first();

$appliedDiscountLines = $appliedDiscount->lines->map(function ($line) {
return $line->line->only('purchasable_type', 'purchasable_id', 'meta');
});

$orderLines = $order->lines->map(function ($line) {
return $line->only('purchasable_type', 'purchasable_id', 'meta');
});

expect($appliedDiscountLines)->toHaveCount($orderLines->count());
expect($appliedDiscountLines->toArray())->toEqual($orderLines->toArray());
});