wir versuchen, per Event OrderEvents::ORDER_TRANSACTION_WRITTEN_EVENT => ‚onOrderTransactionWritten‘ nach einer Bestellung (bzw. Bezahlung) - custom Fields in einer Bestellung zu aktualisieren. (bzw. davor wäre geplant die Daten extern zu übertragen und dann den Status im Custom-Field zu speichern)
Allerdings scheint es hier einen Konflikt mit den Versionen zu geben, da hier zwar in der order Table meine Änderungen der Custom-Fields sichtbar sind, wenn ich dann aber im Backend die Order neu öffne, sehe ich die alten Custom-Field Daten.
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
use Shopware\Core\Checkout\Order\OrderStates;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderStateSubscriber3 implements EventSubscriberInterface {
private $orderRepository;
private $stateMachineStateRepository;
public function __construct(
EntityRepository $orderRepository,
EntityRepository $stateMachineStateRepository
)
{
$this->orderRepository = $orderRepository;
$this->stateMachineStateRepository = $stateMachineStateRepository;
}
public static function getSubscribedEvents(): array {
return [
OrderEvents::ORDER_TRANSACTION_WRITTEN_EVENT => 'onOrderTransactionWritten'
];
}
public function onOrderTransactionWritten (EntityWrittenEvent $event) {
$payloads = $event->getPayloads();
foreach ($payloads as $payload) {
$order_id = $payload["orderId"];
$version_id = $payload["versionId"];
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('id', $order_id)
);
$criteria->addFilter(
new EqualsFilter('versionId', $version_id)
);
$order = $this->orderRepository->search($criteria, $event->getContext())->first();
//get the state of the transaction
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $payload['stateId']));
$data = $this->stateMachineStateRepository->search($criteria, $event->getContext());
if ($data) {
$currentOrderState = $data->first();
$customFields = $order->getCustomFields();
if ($currentOrderState->getTechnicalName() === OrderTransactionStates::STATE_PAID) {
if(!is_array($customFields) || !isset($customFields["crm_submitted"]) || $customFields["crm_submitted"] == false) {
$customFields["crm_submitted"] = true;
$customFields["crm_response_state"] = "Test";
$data = [[
'id' => $order_id,
'versionId' => $version_id,
'customFields' => $customFields
]];
$this->orderRepository->update($data, $event->getContext());
//$order->setCustomFields($customFields);
}
}
}
}
}
}