-
Notifications
You must be signed in to change notification settings - Fork 50
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
Fix state machine exception on order creation #176
Conversation
For all people coming in and looking for quick patch until this is fixed: create class :
<?php
declare(strict_types=1);
namespace App\EventListener\Patch;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;
final class AdminOrderCreationListener
{
/** @var OrderProcessorInterface */
private $orderProcessor;
/** @var FactoryInterface */
private $stateMachineFactory;
public function __construct(OrderProcessorInterface $orderProcessor, FactoryInterface $stateMachineFactory)
{
$this->orderProcessor = $orderProcessor;
$this->stateMachineFactory = $stateMachineFactory;
}
public function processOrderBeforeCreation(GenericEvent $event): void
{
$order = $event->getSubject();
Assert::isInstanceOf($order, OrderInterface::class);
$order->recalculateAdjustmentsTotal();
$this->orderProcessor->process($order);
}
public function completeOrderBeforeCreation(GenericEvent $event): void
{
$order = $event->getSubject();
Assert::isInstanceOf($order, OrderInterface::class);
$stateMachine = $this->stateMachineFactory->get($order, 'sylius_order_checkout');
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
if ($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)) {
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
}
if ($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)) {
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
}
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);
}
} add this to services.yaml: services:
...
Sylius\AdminOrderCreationPlugin\EventListener\OrderCreationListener:
class: App\EventListener\Patch\AdminOrderCreationListener
public: yes
arguments:
- "@sylius.order_processing.order_processor"
- "@sm.factory"
tags:
- { name: "kernel.event_listener", event: "sylius.order.pre_admin_create", method: processOrderBeforeCreation }
- { name: "kernel.event_listener", event: "sylius.order.pre_admin_create", method: completeOrderBeforeCreation }
|
Hey Maciek, thanks a lot for your contribution. Can you fix the failing test? In the best scenario, we should also cover this behaviour with behat. Would you like to give it a try? Best regards |
Hi, I fixed the phpspec tests failing and added additional test for when shipping should be skipped. Hope this helps. Best |
The proposed fix looks good and the tests make sense. Could this PR be merged ? |
When dealing with virtual products or ( when only one shipping method is enabled and channel skipping_shipping_step_allowed )
Thank you, @maciekpaprocki! 🎉 |
This is similar to already existing PR, but i think original poster stopped responding so I can try to get that working.
Issue:
#134
However, the issue also appears when product is not virtual, but skipping_shipping_step_allowed in channel
PR:
#132