Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opsrc 620 add products to cart error 500 fix #148

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/CommandHandler/Wishlist/AddSelectedProductsToCartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItem;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Order\Model\OrderItemInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Order\Modifier\OrderModifierInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
Expand Down Expand Up @@ -49,22 +50,25 @@ public function __construct(
public function __invoke(AddSelectedProductsToCart $addSelectedProductsToCartCommand): void
{
$this->addSelectedProductsToCart($addSelectedProductsToCartCommand->getWishlistProducts());

$this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_selected_wishlist_items_to_cart'));
}

private function addSelectedProductsToCart(Collection $wishlistProducts): void
{
/** @var WishlistItem $wishlistProduct */
foreach ($wishlistProducts as $wishlistProduct) {
if (!$this->isInStock($wishlistProduct)) {
continue;
if ($this->productCanBeProcessed($wishlistProduct)) {
$this->addProductToWishlist($wishlistProduct);
}

$this->addProductToWishlist($wishlistProduct);
}
}

private function productCanBeProcessed(WishlistItem $wishlistProduct): bool
{
$cartItem = $wishlistProduct->getCartItem()->getCartItem();

return $this->isInStock($wishlistProduct) && $this->productHasPositiveQuantity($cartItem);
}

private function isInStock(WishlistItem $wishlistProduct): bool
{
$cartItem = $wishlistProduct->getCartItem()->getCartItem();
Expand All @@ -79,6 +83,16 @@ private function isInStock(WishlistItem $wishlistProduct): bool
return false;
}

private function productHasPositiveQuantity(OrderItemInterface $product): bool
{
if (0 < $product->getQuantity()) {
return true;
}
$this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.increase_quantity'));

return false;
}

private function addProductToWishlist(WishlistItem $wishlistProduct): void
{
$cart = $wishlistProduct->getCartItem()->getCart();
Expand All @@ -90,5 +104,10 @@ private function addProductToWishlist(WishlistItem $wishlistProduct): void

$this->orderModifier->addToOrder($cart, $cartItem);
$this->orderRepository->add($cart);

if (false === $this->flashBag->has('success')){
$this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_to_cart'));
}

}
}
51 changes: 32 additions & 19 deletions src/Controller/Action/BaseWishlistProductsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

abstract class BaseWishlistProductsAction
{
Expand All @@ -39,14 +40,17 @@ abstract class BaseWishlistProductsAction

private WishlistRepositoryInterface $wishlistRepository;

private TranslatorInterface $translator;

public function __construct(
CartContextInterface $cartContext,
FormFactoryInterface $formFactory,
FlashBagInterface $flashBag,
WishlistCommandProcessorInterface $wishlistCommandProcessor,
MessageBusInterface $messageBus,
UrlGeneratorInterface $urlGenerator,
WishlistRepositoryInterface $wishlistRepository
WishlistRepositoryInterface $wishlistRepository,
TranslatorInterface $translator
) {
$this->cartContext = $cartContext;
$this->formFactory = $formFactory;
Expand All @@ -55,22 +59,27 @@ public function __construct(
$this->messageBus = $messageBus;
$this->urlGenerator = $urlGenerator;
$this->wishlistRepository = $wishlistRepository;
$this->translator = $translator;
}

public function __invoke(int $wishlistId, Request $request): Response
{
$form = $this->createForm($wishlistId);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->handleCommand($form);

if ($this->createForm($wishlistId) == null) {
return new RedirectResponse(
$this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_show_chosen_wishlist', [
'wishlistId' => $wishlistId,
])
);
$this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_wishlists'));
} else {
$form = $this->createForm($wishlistId);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->handleCommand($form);

return new RedirectResponse(
$this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_show_chosen_wishlist', [
'wishlistId' => $wishlistId,
])
);
}
}

foreach ($form->getErrors() as $error) {
Expand All @@ -86,15 +95,19 @@ public function __invoke(int $wishlistId, Request $request): Response

abstract protected function handleCommand(FormInterface $form): void;

private function createForm(int $wishlistId): FormInterface
private function createForm(int $wishlistId): ?FormInterface
{
$wishlist = $this->wishlistRepository->find($wishlistId);
$cart = $this->cartContext->getCart();

$commandsArray = $this->wishlistCommandProcessor->createWishlistItemsCollection($wishlist->getWishlistProducts());

return $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
'cart' => $cart,
]);
if ($wishlist == null) {
$this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_not_exists'));
return null;
} else {
$commandsArray = $this->wishlistCommandProcessor->createWishlistItemsCollection($wishlist->getWishlistProducts());

return $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
'cart' => $cart,
]);
}
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- "@sylius.command_bus"
- "@router"
- "@bitbag_sylius_wishlist_plugin.repository.wishlist"
- "@translator"

bitbag_sylius_wishlist_plugin.controller.action.base_wishlists_listing_action:
abstract: true
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ bitbag_sylius_wishlist_plugin:
variant_already_in_wishlist: Wariant znajduje się już w liście życzeń.
selected_wishlist_items_successfully_exported: Wybrane produkty zostały pomyślnie wyeksportowane.
copied_selected_wishlist_items: Wybrane produkty zostały pomyślnie skopiowane.
wishlist_not_exists: Your wishlist does not exists.