Php Shopware6:订单付款时,订阅哪个事件?
我想知道如果我想在客户创建订单后,支付订单时触发功能,我应该使用哪个事件 我已经试过了:state\u enter.order\u transaction.state.paid=>“onordercechkout” 不幸的是,它给出了一个错误: 警告:使用未定义的常量状态输入-假定为“状态输入”(这将在 PHP的未来版本)*” 这是我的订户:Php Shopware6:订单付款时,订阅哪个事件?,php,symfony,events,event-handling,shopware,Php,Symfony,Events,Event Handling,Shopware,我想知道如果我想在客户创建订单后,支付订单时触发功能,我应该使用哪个事件 我已经试过了:state\u enter.order\u transaction.state.paid=>“onordercechkout” 不幸的是,它给出了一个错误: 警告:使用未定义的常量状态输入-假定为“状态输入”(这将在 PHP的未来版本)*” 这是我的订户: namespace Emakers\TransmissionPlugin\Subscriber; use Symfony\Component\Even
namespace Emakers\TransmissionPlugin\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\System\StateMachine\Event;
use Shopware\Core\Framework\Event\EventData\EntityType;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Emakers\TransmissionPlugin\Services\ShopwareConnectService;
class OrderSubscriber implements EventSubscriberInterface
{
/**
* @ContainerInterface $container
*/
private $container;
/**
* @var datetime
*/
private $now;
public function __construct(ContainerInterface $container) {
$this->container = $container;
$this->now = date('Y-m-d H:i:s');
}
public static function getSubscribedEvents(): array
{
return [
'state_enter.order_transaction.state.paid' => 'onOrderCheckout',
];
}
public function orOrderCheckout($event)
{
var_dump($event);
die('here we are');
}
}
我使用StateMachineTransitionEvent进行此操作。使用Shopware 6.2进行测试
<?php declare(strict_types=1);
namespace Your\Namespace;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderStateSubscriber implements EventSubscriberInterface {
/**
* @var EntityRepositoryInterface
*/
private $stateMachineStateRepository;
/**
* @var EntityRepositoryInterface
*/
private $orderRepository;
public function __construct(EntityRepositoryInterface $stateMachineStateRepository, EntityRepositoryInterface $orderRepository)
{
$this->stateMachineStateRepository = $stateMachineStateRepository;
$this->orderRepository = $orderRepository;
}
public static function getSubscribedEvents(): array
{
return [
StateMachineTransitionEvent::class => 'onStateTransition'
];
}
public function onStateTransition(StateMachineTransitionEvent $event) {
// Since you are only interested in order transitions
if ($event->getEntityName() !== OrderTransactionDefinition::ENTITY_NAME) {
return;
}
$orderTransactionsStatePaidId = $this->getOrderTransactionsStatePaidId($event->getContext());
if ($orderTransactionsStatePaidId === null) {
return;
}
// Check if it was transitioned to paid
if ($event->getToPlace()->getId() !== $orderTransactionsStatePaidId) {
return;
}
// Transaction was changed to paid do your thing
$order = $this->getOrder($event->getEntityId(), $event->getContext());
}
private function getOrderTransactionsStatePaidId(Context $context): ?string {
$criteria = new Criteria();
// Add filter for OrderTransactionStateMachine
$criteria->addFilter(
new EqualsFilter('stateMachine.technicalName', \sprintf('%s.state', OrderTransactionDefinition::ENTITY_NAME)),
new EqualsFilter('technicalName', OrderTransactionStates::STATE_PAID)
);
return $this->stateMachineStateRepository->searchIds($criteria, $context)->firstId();
}
private function getOrder(string $orderTransactionId, Context $context): ?OrderEntity
{
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('transactions.id', $orderTransactionId)
);
return $this->orderRepository->search($criteria, $context)->first();
}
}
在名称周围加引号,它已经是正确的:'state\u enter.order\u transaction.state.paid'(在本例中它不是一个PHP常量,而是一个简单的字符串)@Susi我加了引号,但不幸的是,当我下订单并直接付款时,它没有触发ordercheckout()函数。我更新了我的帖子以添加订户的内容,这可能有助于了解问题所在。@UmarZahid我添加了一个示例services.xml条目。谢谢您的回复。我是在问你之后发现的:’)最后一个问题:我需要获得已付款订单的信息,但在倾销$event时。我没有任何orderId或其他东西。在这种情况下有可能得到它们吗?如果我只有orderId,我很高兴你有了属性entityId。这是OrderTransactionId,您可以使用它获取订单,因为它们有直接关联。很抱歉,我现在无法测试,我必须等待同事支付测试费用。但这会是这样的吗?:$entityId=$event->getEntityId();/*@var EntityRepositoryInterface$orderRepository*/$orderRepository=$this->container->get('order.repository')$orderInfo=$orderRepository->SearchID((新标准())->addFilter(新的EqualFilter('orderTransactionId',$entityId)),\Shopware\Core\Framework\Context::createDefaultContext())->GetID()[0]代码>我调整了我的答案,这样你就可以下订单了。
<service id="Your\Namespace\OrderStateSubscriber">
<argument type="service" id="state_machine_state.repository"/>
<argument type="service" id="order.repository"/>
<tag name="kernel.event_subscriber"/>
</service>