From 089f454f30ce3c88be7367ac4f689747ca847d90 Mon Sep 17 00:00:00 2001 From: Steffen Mahler Date: Fri, 21 Aug 2020 10:49:01 +0200 Subject: [PATCH] fix bug in cart item validation result --- .../BookingProduct/src/Helpers/Booking.php | 1 - .../BookingProduct/src/Helpers/EventTicket.php | 1 - .../BookingProduct/src/Helpers/RentalSlot.php | 1 - .../Webkul/BookingProduct/src/Type/Booking.php | 1 + packages/Webkul/Checkout/src/Cart.php | 12 ++++++------ .../src/Datatypes/CartItemValidationResult.php | 18 ++++++++---------- .../Webkul/Product/src/Type/AbstractType.php | 10 ++++------ packages/Webkul/Product/src/Type/Bundle.php | 5 ++--- .../Webkul/Product/src/Type/Configurable.php | 1 - .../Webkul/Product/src/Type/Downloadable.php | 1 - tests/unit/Checkout/CartCest.php | 2 ++ 11 files changed, 23 insertions(+), 30 deletions(-) diff --git a/packages/Webkul/BookingProduct/src/Helpers/Booking.php b/packages/Webkul/BookingProduct/src/Helpers/Booking.php index 6d0df5d9e..54f1de0d5 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/Booking.php +++ b/packages/Webkul/BookingProduct/src/Helpers/Booking.php @@ -556,7 +556,6 @@ class Booking $item->total = core()->convertPrice($price * $item->quantity); $item->save(); - $result->cartIsDirty(); return $result; } diff --git a/packages/Webkul/BookingProduct/src/Helpers/EventTicket.php b/packages/Webkul/BookingProduct/src/Helpers/EventTicket.php index 1c5809b5c..fb5dc60d4 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/EventTicket.php +++ b/packages/Webkul/BookingProduct/src/Helpers/EventTicket.php @@ -173,7 +173,6 @@ class EventTicket extends Booking $item->total = core()->convertPrice($price * $item->quantity); $item->save(); - $result->cartIsDirty(); return $result; } diff --git a/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php index 0a9509719..e48f6b2f8 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php +++ b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php @@ -280,7 +280,6 @@ class RentalSlot extends Booking $item->total = core()->convertPrice($price * $item->quantity); $item->save(); - $result->cartIsDirty(); return $result; } diff --git a/packages/Webkul/BookingProduct/src/Type/Booking.php b/packages/Webkul/BookingProduct/src/Type/Booking.php index 718c126db..5e4696434 100644 --- a/packages/Webkul/BookingProduct/src/Type/Booking.php +++ b/packages/Webkul/BookingProduct/src/Type/Booking.php @@ -276,6 +276,7 @@ class Booking extends Virtual $bookingProduct = $this->getBookingProduct($item->product_id); if (! $bookingProduct) { + $result->cartIsInvalid(); return $result; } diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index fb3f27a57..4c8a56e60 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -592,20 +592,20 @@ class Cart if (! $cart = $this->getCart()) { return false; } - - if (count($cart->items) == 0) { + if (count($cart->items) === 0) { $this->cartRepository->delete($cart->id); return false; } - $isDirty = false; + $isInvalid = false; + foreach ($cart->items as $item) { $validationResult = $item->product->getTypeInstance()->validateCartItem($item); if ($validationResult->isItemInactive()) { $this->removeItem($item->id); - $isDirty = true; + $isInvalid = true; session()->flash('info', __('shop::app.checkout.cart.item.inactive')); } @@ -618,10 +618,10 @@ class Cart 'base_total' => $price * $item->quantity, ], $item->id); - $isDirty |= $validationResult->isCartDirty(); + $isInvalid |= $validationResult->isCartInvalid(); } - return ! $isDirty; + return ! $isInvalid; } /** diff --git a/packages/Webkul/Product/src/Datatypes/CartItemValidationResult.php b/packages/Webkul/Product/src/Datatypes/CartItemValidationResult.php index 5bb00759d..8a4fdae37 100644 --- a/packages/Webkul/Product/src/Datatypes/CartItemValidationResult.php +++ b/packages/Webkul/Product/src/Datatypes/CartItemValidationResult.php @@ -4,21 +4,20 @@ namespace Webkul\Product\Datatypes; class CartItemValidationResult { - /** @var bool $cartIsDirty */ - private $cartIsDirty = false; + /** @var bool $cartIsInvalid */ + private $cartIsInvalid = false; /** @var bool $itemIsInactive */ private $itemIsInactive = false; /** - * Function to check if cart is dirty - * (price has been changed, product was disabled and so on) + * Function to check if cart is invalid * * @return bool */ - public function isCartDirty(): bool + public function isCartInvalid(): bool { - return $this->cartIsDirty; + return $this->cartIsInvalid; } /** @@ -37,14 +36,13 @@ class CartItemValidationResult public function itemIsInactive(): void { $this->itemIsInactive = true; - $this->cartIsDirty = true; } /** - * Function to set if cart is dirty + * Function to set if cart is invalid */ - public function cartIsDirty(): void + public function cartIsInvalid(): void { - $this->cartIsDirty = true; + $this->cartIsInvalid = true; } } diff --git a/packages/Webkul/Product/src/Type/AbstractType.php b/packages/Webkul/Product/src/Type/AbstractType.php index 97f2424ea..fffda16a9 100644 --- a/packages/Webkul/Product/src/Type/AbstractType.php +++ b/packages/Webkul/Product/src/Type/AbstractType.php @@ -5,7 +5,6 @@ namespace Webkul\Product\Type; use Illuminate\Support\Facades\Storage; use Webkul\Attribute\Repositories\AttributeRepository; use Webkul\Checkout\Facades\Cart; -use Webkul\Checkout\Models\CartItem; use Webkul\Product\Datatypes\CartItemValidationResult; use Webkul\Product\Helpers\ProductImage; use Webkul\Product\Models\ProductAttributeValue; @@ -703,7 +702,7 @@ abstract class AbstractType $data = $this->getQtyRequest($data); - if (!$this->haveSufficientQuantity($data['quantity'])) { + if (! $this->haveSufficientQuantity($data['quantity'])) { return trans('shop::app.checkout.cart.quantity.inventory_warning'); } @@ -813,11 +812,11 @@ abstract class AbstractType /** * Validate cart item product price and other things * - * @param CartItem $item + * @param \Webkul\Checkout\Models\CartItem $item * - * @return CartItemValidationResult + * @return \Webkul\Product\Datatypes\CartItemValidationResult */ - public function validateCartItem(CartItem $item): CartItemValidationResult + public function validateCartItem(\Webkul\Checkout\Models\CartItem $item): CartItemValidationResult { $result = new CartItemValidationResult(); @@ -840,7 +839,6 @@ abstract class AbstractType $item->total = core()->convertPrice($price * $item->quantity); $item->save(); - $result->cartIsDirty(); return $result; } diff --git a/packages/Webkul/Product/src/Type/Bundle.php b/packages/Webkul/Product/src/Type/Bundle.php index 9250179c5..d55326104 100644 --- a/packages/Webkul/Product/src/Type/Bundle.php +++ b/packages/Webkul/Product/src/Type/Bundle.php @@ -703,8 +703,8 @@ class Bundle extends AbstractType $result->itemIsInactive(); } - if ($childResult->isCartDirty()) { - $result->cartIsDirty(); + if ($childResult->isCartInvalid()) { + $result->cartIsInvalid(); } $price += $childItem->base_price * $childItem->quantity; @@ -723,7 +723,6 @@ class Bundle extends AbstractType $item->additional = $this->getAdditionalOptions($item->additional); $item->save(); - $result->cartIsDirty(); return $result; } diff --git a/packages/Webkul/Product/src/Type/Configurable.php b/packages/Webkul/Product/src/Type/Configurable.php index 1d799b11c..4fa02f599 100644 --- a/packages/Webkul/Product/src/Type/Configurable.php +++ b/packages/Webkul/Product/src/Type/Configurable.php @@ -571,7 +571,6 @@ class Configurable extends AbstractType $item->total = core()->convertPrice($price * $item->quantity); $item->save(); - $result->cartIsDirty(); return $result; } diff --git a/packages/Webkul/Product/src/Type/Downloadable.php b/packages/Webkul/Product/src/Type/Downloadable.php index ff584f4fe..55e6957f2 100644 --- a/packages/Webkul/Product/src/Type/Downloadable.php +++ b/packages/Webkul/Product/src/Type/Downloadable.php @@ -266,7 +266,6 @@ class Downloadable extends AbstractType $item->total = core()->convertPrice($price * $item->quantity); $item->save(); - $result->cartIsDirty(); return $result; } diff --git a/tests/unit/Checkout/CartCest.php b/tests/unit/Checkout/CartCest.php index 663442f2b..f2d380861 100644 --- a/tests/unit/Checkout/CartCest.php +++ b/tests/unit/Checkout/CartCest.php @@ -60,6 +60,7 @@ class CartCest $I->comment("Check, I'm a guest"); $I->assertFalse(auth()->guard('customer')->check()); + $I->assertNull(cart()->getCart()); $data = [ '_token' => session('_token'), @@ -120,6 +121,7 @@ class CartCest } $I->comment('Again, guest is adding another product of type ' . $product1->type . '.'); + $I->assertNull(cart()->getCart()); cart()->addProduct($product1->id, $data); $I->assertEquals(1, cart()->getCart()->items->count()); $I->assertEquals(2, cart()->getCart()->items_qty);