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

OP-221 - Fix issue with broken add to cart button submit on product page #235

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions src/CommandHandler/Wishlist/CreateWishlistHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Doctrine\Persistence\ObjectManager;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
Expand All @@ -36,20 +37,28 @@ final class CreateWishlistHandler implements MessageHandlerInterface

private TokenUserResolverInterface $tokenUserResolver;

private RequestStack $requestStack;

private string $wishlistCookieToken;

public function __construct(
TokenStorageInterface $tokenStorage,
WishlistFactoryInterface $wishlistFactory,
ShopUserWishlistResolverInterface $shopUserWishlistResolver,
ObjectManager $wishlistManager,
ChannelRepositoryInterface $channelRepository,
TokenUserResolverInterface $tokenUserResolver,
RequestStack $requestStack,
string $wishlistCookieToken,
) {
$this->tokenStorage = $tokenStorage;
$this->wishlistFactory = $wishlistFactory;
$this->shopUserWishlistResolver = $shopUserWishlistResolver;
$this->wishlistManager = $wishlistManager;
$this->channelRepository = $channelRepository;
$this->tokenUserResolver = $tokenUserResolver;
$this->requestStack = $requestStack;
$this->wishlistCookieToken = $wishlistCookieToken;
}

public function __invoke(CreateWishlist $createWishlist): WishlistInterface
Expand All @@ -69,6 +78,8 @@ public function __invoke(CreateWishlist $createWishlist): WishlistInterface
if (null !== $createWishlist->getTokenValue())
{
$wishlist->setToken($createWishlist->getTokenValue());
$mainRequest = $this->requestStack->getMainRequest();
$mainRequest->attributes->set($this->wishlistCookieToken, $createWishlist->getTokenValue());
}

$channelCode = $createWishlist->getChannelCode();
Expand Down
7 changes: 5 additions & 2 deletions src/EventSubscriber/CreateNewWishlistSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public function onKernelRequest(RequestEvent $event): void
}

$currentPath = $this->mainRequest->getPathInfo();
if (!str_starts_with($currentPath, self::ALLOWED_ENDPOINTS_PREFIX)) {
$isWishlistUrl = str_starts_with($currentPath, self::ALLOWED_ENDPOINTS_PREFIX);
if (!$isWishlistUrl) {
return;
}

Expand Down Expand Up @@ -94,8 +95,10 @@ public function onKernelResponse(ResponseEvent $event): void
return;
}

$tokenWasGenerated = $this->mainRequest->attributes->has($this->wishlistCookieToken);
$currentPath = $this->mainRequest->getPathInfo();
if (!str_starts_with($currentPath, self::ALLOWED_ENDPOINTS_PREFIX)) {
$isWishlistUrl = str_starts_with($currentPath, self::ALLOWED_ENDPOINTS_PREFIX);
if (!$tokenWasGenerated && !$isWishlistUrl) {
return;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services/message_handler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ services:
- "@bitbag_sylius_wishlist_plugin.manager.wishlist"
- "@sylius.repository.channel"
- "@bitbag_sylius_wishlist_plugin.resolver.token_user_resolver"
- "@request_stack"
- "%bitbag_sylius_wishlist_plugin.parameters.wishlist_cookie_token%"
tags:
- { name: messenger.message_handler }

Expand Down
Loading