diff --git a/Changelog.md b/Changelog.md index 7c6b138..ed61fb5 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/Contracts/Checkout.php b/Contracts/Checkout.php index 75d60fd..3087833 100644 --- a/Contracts/Checkout.php +++ b/Contracts/Checkout.php @@ -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; diff --git a/Drivers/BaseCheckoutStore.php b/Drivers/BaseCheckoutStore.php index c18bc3b..8397d23 100644 --- a/Drivers/BaseCheckoutStore.php +++ b/Drivers/BaseCheckoutStore.php @@ -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 diff --git a/Drivers/RequestStore.php b/Drivers/RequestStore.php index e1403d6..fd62171 100644 --- a/Drivers/RequestStore.php +++ b/Drivers/RequestStore.php @@ -55,6 +55,8 @@ class RequestStore extends BaseCheckoutStore protected DetailedAmount $taxes; + protected DetailedAmount $promotions; + protected CheckoutRequest $request; /** @var array */ @@ -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); } /** @@ -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); diff --git a/Drivers/SessionStore.php b/Drivers/SessionStore.php index 4ab15a1..d4d759a 100644 --- a/Drivers/SessionStore.php +++ b/Drivers/SessionStore.php @@ -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(); @@ -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);