Search code examples
shopware

How to change order state from open to quote on order created in shopware6?


I am developing a plugin for the quotation request and I am following the order method to create the quote request, I need to change the orderstatus from open to quote as the order is created, I tried to change the stateId but it did not work. The order status at the backend is not changing to the customized one.

<?php declare(strict_types=1);

namespace RequestanOffer\Subscriber;

use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Core\Checkout\Order\OrderEvents;

use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
use Shopware\Core\Checkout\Order\OrderStates;

use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;

use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;


class Subscriber implements EventSubscriberInterface
{
    public const STATE_MACHINE = 'order.state';
    private $orderRepository;
    private $stateMachineStateRepository;
    private CONST ORDER_OPEN = 'D6A9628852A141B9957FFB1A48EE9551';
    private CONST QUOTE_OPEN = '1FEA95C739834229996CEE2D8BEBFDC0';
    public function __construct(
        EntityRepositoryInterface $orderRepository,
        EntityRepositoryInterface $stateMachineRepository
    )
    {
        $this->orderRepository = $orderRepository;
        $this->stateMachineRepository = $stateMachineRepository;
    }


    public static function getSubscribedEvents(): array
    {
        return [
            ProductPageLoadedEvent::class => 'onProductPageLoaded',
            CheckoutCartPageLoadedEvent::class  => 'onCheckoutPageLoaded',
            CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
            OrderEvents::ORDER_WRITTEN_EVENT     => 'onQuoteOrder'
        ];
    }


    public function onQuoteOrder(EntityWrittenEvent $stateId, $event) {



           $stateId = $payloads[0]['stateId'] = $this::QUOTE_OPEN;
        $criteria = new Criteria();
        $criteria->addFilter(
            new EqualsFilter('stateId', $stateId)

        );

               var_dump($payloads[0]['stateId']);
               die('dump');
        }  
}

Solution

  • I solved this problem by adding the Core/StateMachineTransitionActions.php class at my plugin with additional StateMachineTransitionActions.

    public const ACTION_CANCEL = 'cancel';
    public const ACTION_COMPLETE = 'complete';
    public const ACTION_DO_PAY = 'do_pay';
    public const ACTION_FAIL = 'fail';
    public const ACTION_PAID = 'paid';
    public const ACTION_PAID_PARTIALLY = 'paid_partially';
    public const ACTION_PROCESS = 'process';
    public const ACTION_REFUND = 'refund';
    public const ACTION_REFUND_PARTIALLY = 'refund_partially';
    public const ACTION_REMIND = 'remind';
    public const ACTION_REOPEN = 'reopen';
    public const ACTION_RETOUR = 'retour';
    public const ACTION_RETOUR_PARTIALLY = 'retour_partially';
    public const ACTION_SHIP = 'ship';
    public const ACTION_SHIP_PARTIALLY = 'ship_partially';
    public const ACTION_AUTHORIZE = 'authorize';
    public const ACTION_CHARGEBACK = 'chargeback';
    public const ACTION_ACCEPTQUOTE = 'accept_quote';
    public const ACTION_ASKQUOTE = 'ask_quote';
    public const ACTION_QUOTATIONREQUEST = 'quotation_request';
    public const ACTION_QUOTATIONOFFER = 'quotation_offer';