Skip to content

Commit

Permalink
Added hasAnyCoupon, getPromotionsAmount and setPromotionsAmount metho…
Browse files Browse the repository at this point in the history
…ds to checkout
  • Loading branch information
fulopattila122 committed Aug 23, 2024
1 parent d54edaf commit e81ca5b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

## 4.x Series

## Unreleased
##### 2024-XX-YY

- Added the following, v5 interface candidate methods to the Checkout implementations:
- `addCoupon()`
- `removeCoupon()`
- `hasCoupon()`
- `getCoupons()`
- `hasAnyCoupon()`
- `getPromotionsAmount()`
- `setPromotionsAmount()`

## 4.1.0
##### 2024-07-11

Expand Down
3 changes: 3 additions & 0 deletions Contracts/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public function setTaxesAmount(float|DetailedAmount $amount): void;
* public function removeCoupon(string $couponCode): void;
* public function hasCoupon(string $couponCode): bool;
* public function getCoupons(): array;
* public function hasAnyCoupon(): bool;
* public function getPromotionsAmount(): DetailedAmount;
* public function setPromotionsAmount(float|DetailedAmount $amount): void;
*/

public function itemsTotal(): float;
Expand Down
9 changes: 7 additions & 2 deletions Drivers/BaseCheckoutStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,19 @@ public function removeCoupon(string $couponCode): void
Event::dispatch(new CouponRemoved($this, $couponCode));
}

public function hasAnyCoupon(): bool
{
return 0 !== count($this->getCoupons());
}

public function hasCoupon(string $couponCode): bool
{
in_array($couponCode, $this->getCoupons());
return in_array($couponCode, $this->getCoupons());
}

public function getCoupons(): array
{
Arr::wrap($this->readRawDataFromStore('coupons'));
return Arr::wrap($this->readRawDataFromStore('coupons'));
}

public function weight(): float
Expand Down
13 changes: 13 additions & 0 deletions Drivers/RequestStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class RequestStore extends BaseCheckoutStore

protected DetailedAmount $taxes;

protected DetailedAmount $promotions;

protected CheckoutRequest $request;

/** @var array */
Expand All @@ -71,6 +73,7 @@ public function __construct(CheckoutDataFactory $dataFactory, Request|CheckoutRe
}
$this->taxes = new DetailedAmountDto(0);
$this->shippingFees = new DetailedAmountDto(0);
$this->promotions = new DetailedAmountDto(0);
}

/**
Expand Down Expand Up @@ -127,6 +130,16 @@ public function setTaxesAmount(float|DetailedAmount $amount): void
$this->taxes = $amount instanceof DetailedAmount ? $amount : new DetailedAmountDto($amount);
}

public function getPromotionsAmount(): DetailedAmount
{
return $this->promotions;
}

public function setPromotionsAmount(float|DetailedAmount $amount): void
{
$this->promotions = $amount instanceof DetailedAmount ? $amount : new DetailedAmountDto($amount);
}

public function setCustomAttribute(string $key, $value): void
{
Arr::set($this->customData, $key, $value);
Expand Down
27 changes: 27 additions & 0 deletions Drivers/SessionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ public function setTaxesAmount(float|DetailedAmount $amount): void
$this->writeRawDataToStore('taxes', $amount);
}

public function getPromotionsAmount(): DetailedAmount
{
return $this->getDetailedAmountAttribute('promotions');
}

public function setPromotionsAmount(float|DetailedAmount $amount): void
{
$this->setDetailedAmountAttribute('promotions', $amount);
}

public function setCustomAttribute(string $key, $value): void
{
$customAttributes = $this->getCustomAttributes();
Expand Down Expand Up @@ -140,6 +150,23 @@ public function clear(): void
$this->session->forget($this->prefix);
}

public function getDetailedAmountAttribute(string $name): DetailedAmount
{
$raw = $this->readRawDataFromStore($name, []);

return is_array($raw) ?
DetailedAmountDto::fromArray($raw) : new DetailedAmountDto(is_numeric($raw) ? floatval($raw) : 0);
}

public function setDetailedAmountAttribute(string $name, float|DetailedAmount $amount): void
{
if ($amount instanceof DetailedAmount && empty($amount->getDetails())) {
$amount = $amount->getValue();//Otherwise the value gets lost
}

$this->writeRawDataToStore($name, $amount);
}

protected function readRawDataFromStore(string $key, $default = null): mixed
{
return $this->session->get($this->prefix . '.' . $key, $default);
Expand Down

0 comments on commit e81ca5b

Please sign in to comment.