fix bug in cart item validation result
This commit is contained in:
parent
5e651c677e
commit
089f454f30
|
|
@ -556,7 +556,6 @@ class Booking
|
|||
$item->total = core()->convertPrice($price * $item->quantity);
|
||||
|
||||
$item->save();
|
||||
$result->cartIsDirty();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -173,7 +173,6 @@ class EventTicket extends Booking
|
|||
$item->total = core()->convertPrice($price * $item->quantity);
|
||||
|
||||
$item->save();
|
||||
$result->cartIsDirty();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -280,7 +280,6 @@ class RentalSlot extends Booking
|
|||
$item->total = core()->convertPrice($price * $item->quantity);
|
||||
|
||||
$item->save();
|
||||
$result->cartIsDirty();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -276,6 +276,7 @@ class Booking extends Virtual
|
|||
$bookingProduct = $this->getBookingProduct($item->product_id);
|
||||
|
||||
if (! $bookingProduct) {
|
||||
$result->cartIsInvalid();
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -571,7 +571,6 @@ class Configurable extends AbstractType
|
|||
$item->total = core()->convertPrice($price * $item->quantity);
|
||||
|
||||
$item->save();
|
||||
$result->cartIsDirty();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -266,7 +266,6 @@ class Downloadable extends AbstractType
|
|||
$item->total = core()->convertPrice($price * $item->quantity);
|
||||
|
||||
$item->save();
|
||||
$result->cartIsDirty();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue