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

Addition of getCartQuantity method on the CartSessionManager #2072

Open
wants to merge 3 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
42 changes: 28 additions & 14 deletions packages/core/src/Base/Traits/HasCustomerGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public static function bootHasCustomerGroups(): void
static::created(function (Model $model) {
$model->customerGroups()->sync(
CustomerGroup::get()->mapWithKeys(
fn ($customerGroup): array => [$customerGroup->id => [
'enabled' => $customerGroup->default,
'starts_at' => now(),
'ends_at' => null,
'visible' => $customerGroup->default,
...static::getExtraCustomerGroupPivotValues($customerGroup),
]]
fn ($customerGroup): array => [
$customerGroup->id => [
'enabled' => $customerGroup->default,
'starts_at' => now(),
'ends_at' => null,
'visible' => $customerGroup->default,
...static::getExtraCustomerGroupPivotValues($customerGroup),
],
]
)
);
});
Expand Down Expand Up @@ -94,13 +96,20 @@ protected function validateScheduling(Collection $models)
* @param Collection $customerGroups
* @return Builder
*/
public function applyCustomerGroupScope(Builder $query, Collection $groupIds, DateTime $startsAt, DateTime $endsAt)
{
return $query->whereHas('customerGroups', function ($relation) use ($groupIds, $startsAt, $endsAt) {
public function applyCustomerGroupScope(
Builder $query,
Collection $groupIds,
DateTime $startsAt,
DateTime $endsAt,
bool $onlyVisible
) {
return $query->whereHas('customerGroups', function ($relation) use ($groupIds, $startsAt, $endsAt, $onlyVisible) {
$relation->whereIn(
$this->customerGroups()->getTable().'.customer_group_id',
$groupIds
)->where(function ($query) use ($startsAt) {
)->when($onlyVisible, function ($query) {
$query->where('visible', true);
})->where(function ($query) use ($startsAt) {
$query->whereNull('starts_at')
->orWhere('starts_at', '<=', $startsAt);
})->where(function ($query) use ($endsAt) {
Expand All @@ -120,8 +129,13 @@ public function applyCustomerGroupScope(Builder $query, Collection $groupIds, Da
* @param CustomerGroup|string $customerGroup
* @return Builder
*/
public function scopeCustomerGroup($query, CustomerGroup|iterable|null $customerGroup = null, ?DateTime $startsAt = null, ?DateTime $endsAt = null)
{
public function scopeCustomerGroup(
$query,
CustomerGroup|iterable|null $customerGroup = null,
?DateTime $startsAt = null,
?DateTime $endsAt = null,
?bool $onlyVisible = false
) {
if (blank($customerGroup)) {
return $query;
}
Expand All @@ -148,6 +162,6 @@ public function scopeCustomerGroup($query, CustomerGroup|iterable|null $customer
$endsAt = now()->addSecond();
}

return $this->applyCustomerGroupScope($query, $groupIds, $startsAt, $endsAt);
return $this->applyCustomerGroupScope($query, $groupIds, $startsAt, $endsAt, $onlyVisible);
}
}
34 changes: 31 additions & 3 deletions packages/core/src/Managers/CartSessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Illuminate\Session\SessionManager;
use Illuminate\Support\Collection;
use Lunar\Base\CartSessionInterface;
use Lunar\Facades\ModelManifest;
use Lunar\Facades\ShippingManifest;
use Lunar\Models\Cart;
use Lunar\Models\CartLine;
use Lunar\Models\Channel;
use Lunar\Models\Currency;
use Lunar\Models\Order;
use Lunar\Models\ProductVariant;

class CartSessionManager implements CartSessionInterface
{
Expand Down Expand Up @@ -81,7 +84,6 @@ public function forget(bool $delete = true): void
$this->sessionManager->forget(
$this->getSessionKey()
);

}

/**
Expand Down Expand Up @@ -120,9 +122,10 @@ public function use(Cart $cart): Cart
}

/**
* Fetches a cart and optionally creates one if it doesn't exist.
* Retrieve the ID for the current session cart.
* Returns null if one doesn't exist.
*/
private function fetchOrCreate(bool $create = false, bool $estimateShipping = false, bool $calculate = true): ?Cart
public function getCartId(): ?int
{
$cartId = $this->sessionManager->get(
$this->getSessionKey()
Expand All @@ -132,6 +135,31 @@ private function fetchOrCreate(bool $create = false, bool $estimateShipping = fa
$cartId = $user->carts()->active()->first()?->id;
}

return $cartId;
}

/**
* Retrieve the total number of items in the cart.
*/
public function getCartQuantity(): int
{
if (! $cartId = $this->getCartId()) {
return 0;
}

return CartLine::query()
->where('cart_id', $cartId)
->where('purchasable_type', ModelManifest::getMorphMapKey(ProductVariant::class))
->sum('quantity') ?? 0;
}

/**
* Fetches a cart and optionally creates one if it doesn't exist.
*/
private function fetchOrCreate(bool $create = false, bool $estimateShipping = false, bool $calculate = true): ?Cart
{
$cartId = $this->getCartId();

if (! $cartId) {
return $create ? $this->cart = $this->createNewCart() : null;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/core/Unit/Managers/CartSessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,33 @@

});

test('can get cart quantity', function () {
Currency::factory()->create([
'default' => true,
]);

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

// create a purchasable product
$product = \Lunar\Models\ProductVariant::factory()
->has(\Lunar\Models\Price::factory())
->create();
$product->purchasable = 'always';

Config::set('lunar.cart_session.auto_create', true);

// add the item to our cart 4 times
$cart = CartSession::current();
$cart->add($product, 4);

// and check the quantity matches
$quantity = app(CartSessionManager::class)->getCartQuantity();

expect($quantity)->toEqual(4);
});

test('can forget a cart and soft delete it', function () {
Currency::factory()->create([
'default' => true,
Expand Down