Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 02c2671
Author: Alec Ritson <hello@itsalec.co.uk>
Date:   Fri Feb 10 11:13:49 2023 +0000

    Update lines.blade.php (#849)

commit 0780a1c
Author: wychoong <67364036+wychoong@users.noreply.github.com>
Date:   Fri Feb 10 18:41:36 2023 +0800

    Update CollectionTree.php (#835)

    Co-authored-by: Alec Ritson <hello@itsalec.co.uk>

commit e558799
Author: Alec Ritson <hello@itsalec.co.uk>
Date:   Fri Feb 10 09:47:16 2023 +0000

    :beer:

commit 8fd4842
Merge: df6f7f7 51af420
Author: Alec Ritson <hello@itsalec.co.uk>
Date:   Fri Feb 10 09:47:03 2023 +0000

    Merge branch '0.2' of github.com:lunarphp/lunar into 0.2

commit df6f7f7
Author: Alec Ritson <hello@itsalec.co.uk>
Date:   Fri Feb 10 09:47:00 2023 +0000

    :beer:

commit 51af420
Author: wychoong <67364036+wychoong@users.noreply.github.com>
Date:   Fri Feb 10 17:43:10 2023 +0800

    [0.2] Hotfix - fix pluck on null (#834)

    * fix pluck on null

    * Update AbstractDiscount.php

    ---------

    Co-authored-by: Alec Ritson <hello@itsalec.co.uk>

commit 7b5ce85
Author: wychoong <67364036+wychoong@users.noreply.github.com>
Date:   Fri Feb 10 17:16:50 2023 +0800

    [0.2] Fix/discount by product (#841)

    * fix

    * add test

    * fix discount minSpend precision

    * fix discount condition logic

    * fix

    * fix coupon check

commit 72f8454
Author: Alec Ritson <hello@itsalec.co.uk>
Date:   Fri Feb 10 09:16:16 2023 +0000

    Hotfix [0.2] - Wrap customer group in array (#846)

    * Wrap customer group in array

    * Update ProductVariantTest.php

commit b8773d3
Author: Ryan Mitchell <ryan@thoughtcollective.com>
Date:   Wed Feb 8 20:43:09 2023 +0000

    When DB::table() is used make sure connection is passed (#826)

commit 650d9db
Merge: dcd39cc de20aff
Author: Alec Ritson <hello@itsalec.co.uk>
Date:   Wed Feb 1 09:57:04 2023 +0000

    Merge branch 'main' into 0.2

commit dcd39cc
Author: Alec Ritson <hello@itsalec.co.uk>
Date:   Tue Jan 31 07:29:15 2023 +0000

    :beer:
  • Loading branch information
alecritson committed Feb 10, 2023
1 parent de20aff commit 10ac7aa
Show file tree
Hide file tree
Showing 18 changed files with 584 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@endif

<div class="flex-shrink-0">
@if ($thumbnail = $line->purchasable->getThumbnail())
@if ($thumbnail = $line->purchasable?->getThumbnail())
<x-hub::thumbnail :src="$thumbnail->getUrl('small')" />
@else
<x-hub::icon ref="photograph"
Expand Down Expand Up @@ -94,7 +94,7 @@ class="w-6 mx-1 text-gray-400 -mt-7 group-hover:text-gray-500 xl:mt-0" />
<div class="flex text-xs font-medium text-gray-600">
<p>{{ $line->identifier }}</p>

@if ($line->purchasable->getOptions()->count())
@if ($line->purchasable?->getOptions()?->count())
<dl class="flex before:content-['|'] before:mx-3 before:text-gray-200 space-x-3">
@foreach ($line->purchasable->getOptions() as $option)
<div class="flex gap-0.5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public function sort($payload)
$objectIdPositions = array_flip($ids);

$models = Collection::withCount('children')
->findMany($ids)
->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getKey()];
})->values();
->findMany($ids)
->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getKey()];
})->values();

Collection::rebuildSubtree(
$models->first()->parent,
Expand Down Expand Up @@ -151,7 +151,7 @@ public function collectionMoved($id)
{
$parentId = collect($this->nodes)->first()['parent_id'];
$this->nodes = $this->mapCollections(
Collection::whereParentId($parentId)->withCount('children')->defaultOrder()->get()
Collection::whereParentId($parentId)->inGroup($this->owner->id)->withCount('children')->defaultOrder()->get()
);
}

Expand All @@ -168,7 +168,7 @@ public function collectionsChanged($parentId)

if ($parentMatched) {
$this->nodes = $this->mapCollections(
Collection::whereParentId($parentId)->withCount('children')->defaultOrder()->get()
Collection::whereParentId($parentId)->inGroup($this->owner->id)->withCount('children')->defaultOrder()->get()
);
}

Expand All @@ -177,7 +177,7 @@ public function collectionsChanged($parentId)

if ($nodeMatched) {
$this->nodes = $this->mapCollections(
Collection::whereParentId($nodeMatched['parent_id'])->withCount('children')->defaultOrder()->get()
Collection::whereParentId($nodeMatched['parent_id'])->inGroup($this->owner->id)->withCount('children')->defaultOrder()->get()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ public function mount()
return $this->mapProductToArray($limitation->purchasable);
});

$this->selectedBrands = $this->discount->brands->map(fn ($brand) => $this->mapBrandToArray($brand)) ?? collect();
$this->selectedCollections = $this->discount->collections->map(fn ($collection) => $this->mapCollectionToArray($collection)) ?? collect();

$this->selectedConditions = $this->discount->purchasableConditions()
->wherePurchasableType(Product::class)
->pluck('purchasable_id')->values()->toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function mount()

$this->selectedBrands = collect();
$this->selectedCollections = collect();
$this->selectedProducts = collect();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public function build()
SelectFilter::make('tags')->options(function () {
$tagTable = (new Tag)->getTable();

$tags = DB::table(
config('lunar.database.table_prefix').'taggables'
)->join($tagTable, 'tag_id', '=', "{$tagTable}.id")
$tags = DB::connection(config('lunar.database.connection'))
->table(config('lunar.database.table_prefix').'taggables')
->join($tagTable, 'tag_id', '=', "{$tagTable}.id")
->whereTaggableType(Order::class)
->distinct()
->pluck('value')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ public function deleteGroup()
return;
}
DB::transaction(function () {
DB::table(config('lunar.database.table_prefix').'attributables')
DB::connection(config('lunar.database.connection'))
->table(config('lunar.database.table_prefix').'attributables')
->whereIn(
'attribute_id',
$this->attributeGroupToDelete->attributes()->pluck('id')->toArray()
Expand All @@ -293,7 +294,8 @@ public function deleteGroup()
public function deleteAttribute()
{
DB::transaction(function () {
DB::table(config('lunar.database.table_prefix').'attributables')
DB::connection(config('lunar.database.connection'))
->table(config('lunar.database.table_prefix').'attributables')
->where(
'attribute_id',
$this->attributeToDelete->id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function getTaggablesProperty()
{
$prefix = config('lunar.database.table_prefix');

return DB::table(
return DB::connection(config('lunar.database.connection'))
->table(
"{$prefix}taggables"
)->select([
'taggable_type',
Expand Down
3 changes: 2 additions & 1 deletion packages/admin/src/Http/Livewire/Components/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function getAvailableTagsProperty()
return collect();
}

return DB::table(
return DB::connection(config('lunar.database.connection'))
->table(
config('lunar.database.table_prefix').'taggables'
)->join($tagTable, 'tag_id', '=', "{$tagTable}.id")
->whereTaggableType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function component_has_system_attributes_preselected()

/**
* @test
*
* @group foo
* */
public function can_populate_product_type_data_and_attributes()
Expand Down
1 change: 0 additions & 1 deletion packages/core/database/factories/CartFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function definition(): array
'merged_id' => null,
'currency_id' => Currency::factory(),
'channel_id' => Channel::factory(),
'coupon_code' => $this->faker->boolean ? $this->faker->word : null,
'completed_at' => null,
'meta' => [],
];
Expand Down
39 changes: 39 additions & 0 deletions packages/core/src/DiscountTypes/AbstractDiscountType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Lunar\DiscountTypes;

use Illuminate\Support\Collection;
use Lunar\Base\DiscountTypeInterface;
use Lunar\Models\Cart;
use Lunar\Models\Discount;

abstract class AbstractDiscountType implements DiscountTypeInterface
Expand Down Expand Up @@ -38,4 +40,41 @@ public function markAsUsed(): self

return $this;
}

/**
* Return the eligible lines for the discount.
*
* @param Cart $cart
* @return Illuminate\Support\Collection
*/
protected function getEligibleLines(Cart $cart): Collection
{
return $cart->lines;
}

/**
* Check if discount's conditions met.
*
* @param Cart $cart
* @return bool
*/
protected function checkDiscountConditions(Cart $cart): bool
{
$data = $this->discount->data;

$cartCoupon = strtoupper($cart->coupon_code ?? null);
$conditionCoupon = strtoupper($this->discount->coupon ?? null);

$validCoupon = $cartCoupon ? ($cartCoupon === $conditionCoupon) : blank($conditionCoupon);

$minSpend = $data['min_prices'][$cart->currency->code] ?? null;
$minSpend = (int) bcmul($minSpend, $cart->currency->factor);

$lines = $this->getEligibleLines($cart);
$validMinSpend = $minSpend ? $minSpend < $lines->sum('subTotal.value') : true;

$validMaxUses = $this->discount->max_uses ? $this->discount->uses < $this->discount->max_uses : true;

return $validCoupon && $validMinSpend && $validMaxUses;
}
}
17 changes: 4 additions & 13 deletions packages/core/src/DiscountTypes/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,7 @@ public function apply(Cart $cart): Cart
{
$data = $this->discount->data;

$cartCoupon = strtoupper($cart->coupon_code ?? null);
$conditionCoupon = strtoupper($this->discount->coupon ?? null);

$passes = $cartCoupon && ($cartCoupon === $conditionCoupon);

$minSpend = $data['min_prices'][$cart->currency->code] ?? null;

$lines = $this->getEligibleLines($cart);

if (! $passes || ($minSpend && $minSpend >= $lines->sum('subTotal.value'))) {
if (! $this->checkDiscountConditions($cart)) {
return $cart;
}

Expand Down Expand Up @@ -65,7 +56,7 @@ private function applyFixedValue(array $values, Cart $cart): Cart
{
$currency = $cart->currency;

$value = ($values[$currency->code] ?? 0) * 100;
$value = (int) bcmul($values[$currency->code] ?? 0, $currency->factor);

$lines = $this->getEligibleLines($cart);

Expand All @@ -92,9 +83,9 @@ private function applyFixedValue(array $values, Cart $cart): Cart
* Return the eligible lines for the discount.
*
* @param Cart $cart
* @return Collection
* @return \Illuminate\Support\Collection
*/
private function getEligibleLines(Cart $cart)
protected function getEligibleLines(Cart $cart): \Illuminate\Support\Collection
{
$collectionIds = $this->discount->collections->pluck('id');
$brandIds = $this->discount->brands->pluck('id');
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Managers/PricingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ public function get()
}

if (! $this->customerGroups || ! $this->customerGroups->count()) {
$this->customerGroups = collect(
CustomerGroup::getDefault()
);
$this->customerGroups = collect([
CustomerGroup::getDefault(),
]);
}

// Do we have a user?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function can_schedule_using_collection_of_models()

/**
* @test
*
* @group testerr
* */
public function throws_exception_if_non_customer_group_provided()
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/Unit/DiscountTypes/BuyXGetYTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public function can_discount_eligible_product()

/**
* @test
*
* @group thisthis
*/
public function can_discount_eligible_products()
Expand Down
Loading

0 comments on commit 10ac7aa

Please sign in to comment.