Skip to content

Commit

Permalink
Add availibility check for product
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed May 20, 2020
1 parent d5c273a commit d3b2357
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/Handler/Cart/PutSimpleItemToCartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
use Sylius\ShopApiPlugin\Checker\ProductInCartChannelCheckerInterface;
use Sylius\ShopApiPlugin\Command\Cart\PutSimpleItemToCart;
use Sylius\ShopApiPlugin\Modifier\OrderModifierInterface;
Expand All @@ -27,16 +28,21 @@ final class PutSimpleItemToCartHandler
/** @var ProductInCartChannelCheckerInterface */
private $channelChecker;

/** @var AvailabilityCheckerInterface */
private $availabilityChecker;

public function __construct(
OrderRepositoryInterface $cartRepository,
ProductRepositoryInterface $productRepository,
OrderModifierInterface $orderModifier,
ProductInCartChannelCheckerInterface $channelChecker
ProductInCartChannelCheckerInterface $channelChecker,
AvailabilityCheckerInterface $availabilityChecker
) {
$this->cartRepository = $cartRepository;
$this->productRepository = $productRepository;
$this->orderModifier = $orderModifier;
$this->channelChecker = $channelChecker;
$this->availabilityChecker = $availabilityChecker;
}

public function __invoke(PutSimpleItemToCart $putSimpleItemToCart): void
Expand All @@ -54,6 +60,9 @@ public function __invoke(PutSimpleItemToCart $putSimpleItemToCart): void

$productVariant = $product->getVariants()[0];

$this->orderModifier->modify($cart, $productVariant, $putSimpleItemToCart->quantity());
$quantity = $putSimpleItemToCart->quantity();
Assert::true($this->availabilityChecker->isStockSufficient($productVariant, $quantity), 'Product variant is not available in stock');

$this->orderModifier->modify($cart, $productVariant, $quantity);
}
}
13 changes: 11 additions & 2 deletions src/Handler/Cart/PutVariantBasedConfigurableItemToCartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
use Sylius\ShopApiPlugin\Checker\ProductInCartChannelCheckerInterface;
use Sylius\ShopApiPlugin\Command\Cart\PutVariantBasedConfigurableItemToCart;
use Sylius\ShopApiPlugin\Modifier\OrderModifierInterface;
Expand All @@ -27,16 +28,21 @@ final class PutVariantBasedConfigurableItemToCartHandler
/** @var ProductInCartChannelCheckerInterface */
private $channelChecker;

/** @var AvailabilityCheckerInterface */
private $availabilityChecker;

public function __construct(
OrderRepositoryInterface $cartRepository,
ProductVariantRepositoryInterface $productVariantRepository,
OrderModifierInterface $orderModifier,
ProductInCartChannelCheckerInterface $channelChecker
ProductInCartChannelCheckerInterface $channelChecker,
AvailabilityCheckerInterface $availabilityChecker
) {
$this->cartRepository = $cartRepository;
$this->productVariantRepository = $productVariantRepository;
$this->orderModifier = $orderModifier;
$this->channelChecker = $channelChecker;
$this->availabilityChecker = $availabilityChecker;
}

public function __invoke(PutVariantBasedConfigurableItemToCart $putConfigurableItemToCart): void
Expand All @@ -55,6 +61,9 @@ public function __invoke(PutVariantBasedConfigurableItemToCart $putConfigurableI

Assert::true($this->channelChecker->isProductInCartChannel($product, $cart), 'Product is not in same channel as cart');

$this->orderModifier->modify($cart, $productVariant, $putConfigurableItemToCart->quantity());
$quantity = $putConfigurableItemToCart->quantity();
Assert::true($this->availabilityChecker->isStockSufficient($productVariant, $quantity), 'Product variant is not available in stock');

$this->orderModifier->modify($cart, $productVariant, $quantity);
}
}
2 changes: 2 additions & 0 deletions src/Resources/config/services/handler/cart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<argument type="service" id="sylius.repository.product"/>
<argument type="service" id="sylius.shop_api_plugin.modifier.order_modifier"/>
<argument type="service" id="sylius.shop_api_plugin.checker.product_in_cart_channel_checker" />
<argument type="service" id="sylius.availability_checker"/>
<tag name="messenger.message_handler" bus="sylius_shop_api_plugin.command_bus"/>
</service>

Expand All @@ -40,6 +41,7 @@
<argument type="service" id="sylius.repository.product_variant"/>
<argument type="service" id="sylius.shop_api_plugin.modifier.order_modifier"/>
<argument type="service" id="sylius.shop_api_plugin.checker.product_in_cart_channel_checker" />
<argument type="service" id="sylius.availability_checker"/>
<tag name="messenger.message_handler" bus="sylius_shop_api_plugin.command_bus"/>
</service>

Expand Down

0 comments on commit d3b2357

Please sign in to comment.