Skip to content

Commit

Permalink
Merge pull request #235 from BitBagCommerce/hotfix/add-to-cart-issue
Browse files Browse the repository at this point in the history
OP-221 - Fix issue with broken add to cart button submit on product page
  • Loading branch information
senghe committed Nov 14, 2023
2 parents 51c4c60 + ac2fae7 commit 0a664fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/CommandHandler/Wishlist/CreateWishlistHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
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;
use Webmozart\Assert\Assert;

final class CreateWishlistHandler implements MessageHandlerInterface
{
Expand All @@ -36,20 +38,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 +79,10 @@ public function __invoke(CreateWishlist $createWishlist): WishlistInterface
if (null !== $createWishlist->getTokenValue())
{
$wishlist->setToken($createWishlist->getTokenValue());
$mainRequest = $this->requestStack->getMainRequest();

Assert::notNull($mainRequest, 'The handler is destined to HTTP context only');
$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

0 comments on commit 0a664fa

Please sign in to comment.