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

Fix state machine exception on order creation #176

Merged
merged 3 commits into from
Feb 25, 2022

Conversation

maciekpaprocki
Copy link
Contributor

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

@maciekpaprocki maciekpaprocki requested a review from a team as a code owner October 13, 2021 19:42
@maciekpaprocki
Copy link
Contributor Author

For all people coming in and looking for quick patch until this is fixed:

create class :

src/EventListener/Patch

<?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 }

@lchrusciel
Copy link
Member

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

@maciekpaprocki
Copy link
Contributor Author

Hi,

I fixed the phpspec tests failing and added additional test for when shipping should be skipped. Hope this helps.

Best

@jdubuisson
Copy link

The proposed fix looks good and the tests make sense. Could this PR be merged ?

.phpunit.result.cache Outdated Show resolved Hide resolved
maciekpaprocki and others added 3 commits February 25, 2022 13:55
When dealing with virtual products or  ( when only one shipping method is enabled and channel skipping_shipping_step_allowed )
@GSadee GSadee added Bug Confirmed bugs or bugfixes. Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.). labels Feb 25, 2022
@GSadee GSadee merged commit 98bbfcb into Sylius:master Feb 25, 2022
@GSadee
Copy link
Member

GSadee commented Feb 25, 2022

Thank you, @maciekpaprocki! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Confirmed bugs or bugfixes. Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants