如何将在 shopware 6 中创建的订单的订单状态从打开更改为报价?

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

我正在为报价请求开发一个插件,我正在按照订单方法创建报价请求,我需要在创建订单时将订单状态从打开更改为报价,我尝试更改 stateId 但是它不起作用。后台的订单状态没有变成自定义的。

<?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');
        }  
}

在您的代码中,您没有保存任何内容。请查看状态机文档,这是如何实现的:

https://developer.shopware.com/docs/guides/plugins/plugins/checkout/order/using-the-state-machine

我通过在我的插件中添加 Core/StateMachineTransitionActions.php class 和额外的 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';