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

[Bug]: Every item added to cart via API is treated as a gift card #261

Open
TalkMarc opened this issue Dec 4, 2023 · 2 comments
Open

[Bug]: Every item added to cart via API is treated as a gift card #261

TalkMarc opened this issue Dec 4, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@TalkMarc
Copy link

TalkMarc commented Dec 4, 2023

Plugin version

0.12.0-alpha.5

Sylius version

1.12.10

What happened?

When a product is added to a cart via API (POST /api/v2/shop/orders/[TOKEN]/items) a new gift card with the value of that product is created, even if the product added is not a gift card.

This also creates follow up issues when an item already existing in a cart is added again. (A new entity was found through the relationship...)

Details

I see that the api platform data persister dispatches a message event with the Message of type Setono\SyliusGiftCardPlugin\Api\Command\AddItemToCart. This message then gets picked up by the plugins AddItemToCartHandler. This handler does not check if the item/product added is a gift card. It only checks if the passed message is of type Setono\SyliusGiftCardPlugin\Api\Command\AddItemToCart, which it always will be, since that is the "input" that is has to be changed in the Order api resource configuration during plugin installation.

API resource config deviation

I find it important to add that, in the installation instructions for the API, the "shop_add_item" operation of the Order has the method "PATCH", while in the Sylius api bundle it's actually configured with method "POST".

Relevant log output

STACK TRACE:
GiftCard.php:57, Setono\SyliusGiftCardPlugin\Model\GiftCard->__construct()
Factory.php:38, Sylius\Component\Resource\Factory\Factory->createNew()
GiftCardFactory.php:50, Setono\SyliusGiftCardPlugin\Factory\GiftCardFactory->createNew()
GiftCardFactory.php:58, Setono\SyliusGiftCardPlugin\Factory\GiftCardFactory->createForChannel()
GiftCardFactory.php:110, Setono\SyliusGiftCardPlugin\Factory\GiftCardFactory->createFromOrderItemUnitAndCart()
AddItemToCartHandler.php:91, Setono\SyliusGiftCardPlugin\Api\CommandHandler\AddItemToCartHandler->__invoke()
HandleMessageMiddleware.php:157, Symfony\Component\Messenger\Middleware\HandleMessageMiddleware->callHandler()
HandleMessageMiddleware.php:96, Symfony\Component\Messenger\Middleware\HandleMessageMiddleware->handle()
SendMessageMiddleware.php:77, Symfony\Component\Messenger\Middleware\SendMessageMiddleware->handle()
DoctrineTransactionMiddleware.php:31, Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware->handleForManager()
AbstractDoctrineMiddleware.php:45, Symfony\Bridge\Doctrine\Messenger\AbstractDoctrineMiddleware->handle()
ValidationMiddleware.php:45, Symfony\Component\Messenger\Middleware\ValidationMiddleware->handle()
FailedMessageProcessingMiddleware.php:34, Symfony\Component\Messenger\Middleware\FailedMessageProcessingMiddleware->handle()
DispatchAfterCurrentBusMiddleware.php:68, Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware->handle()
RejectRedeliveredMessageMiddleware.php:41, Symfony\Component\Messenger\Middleware\RejectRedeliveredMessageMiddleware->handle()
AddBusNameStampMiddleware.php:37, Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware->handle()
TraceableMiddleware.php:40, Symfony\Component\Messenger\Middleware\TraceableMiddleware->handle()
MessageBus.php:70, Symfony\Component\Messenger\MessageBus->dispatch()
TraceableMessageBus.php:38, Symfony\Component\Messenger\TraceableMessageBus->dispatch()
DispatchTrait.php:44, ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister->dispatch()
DataPersister.php:99, ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister->persist()
MessengerDataPersister.php:35, Sylius\Bundle\ApiBundle\DataPersister\MessengerDataPersister->persist()
ChainDataPersister.php:59, ApiPlatform\Core\DataPersister\ChainDataPersister->persist()
TraceableChainDataPersister.php:58, ApiPlatform\Core\Bridge\Symfony\Bundle\DataPersister\TraceableChainDataPersister->persist()
WriteListener.php:96, ApiPlatform\Core\EventListener\WriteListener->onKernelView()
WrappedListener.php:116, Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke()
EventDispatcher.php:220, Symfony\Component\EventDispatcher\EventDispatcher->callListeners()
EventDispatcher.php:56, Symfony\Component\EventDispatcher\EventDispatcher->dispatch()
TraceableEventDispatcher.php:139, Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch()
HttpKernel.php:187, Symfony\Component\HttpKernel\HttpKernel->handleRaw()
HttpKernel.php:76, Symfony\Component\HttpKernel\HttpKernel->handle()
Kernel.php:197, Symfony\Component\HttpKernel\Kernel->handle()
HttpKernelRunner.php:35, Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
autoload_runtime.php:29, require_once()
index.php:5, {main}()
@TalkMarc TalkMarc added the bug Something isn't working label Dec 4, 2023
TalkMarc added a commit to TalkMarc/SyliusGiftCardPlugin that referenced this issue Jan 31, 2024
Only do gift card stuff if a gift card is added to the cart via api.
Fixes Setono#261 (Setono#261)
@loevgaard
Copy link
Member

I will be working on these issues in the upcoming weeks. The first PR I did today where I dropped support for PHP 7.4 and PHP 8.0 to make it easier for me.

@TalkMarc
Copy link
Author

Thank you for the update. You can also check out my simple approach to fix this issue on my fork TalkMarc@bfa384b

ehibes added a commit to ehibes/SyliusGiftCardPlugin that referenced this issue Oct 13, 2024
ehibes added a commit to ehibes/SyliusGiftCardPlugin that referenced this issue Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants