Sylius:附加结帐步骤缺少侦听器
Sylius: Additional checkout step missing listener
我想在 sylius 结帐流程中添加一个结帐步骤。它的工作原理是,当我在购物车概览中并单击结帐时,会显示我添加的步骤。但是,当我单击“下一步”时,出现错误“无法为命名路由“sylius_order_index”生成 URL,因为这样的路由不存在。”
到目前为止我做了什么(现在为了避免分心,我将结帐步骤称为“测试”):
config/packages/_sylius.yaml:
winzou_state_machine:
sylius_order_checkout:
states:
test_selected: ~
transitions:
select_test:
from: [cart, test_selected, addressed, shipping_selected, shipping_skipped, payment_selected, payment_skipped]
to: test_selected
address:
from: [cart, test_selected, addressed, shipping_selected, shipping_skipped, payment_selected, payment_skipped]
to: addressed
select_shipping:
from: [addressed, test_selected, shipping_selected, payment_selected, payment_skipped]
to: shipping_selected
select_payment:
from: [payment_selected, test_selected, shipping_skipped, shipping_selected]
to: payment_selected
callbacks:
after:
sylius_process_cart:
on: ["select_shipping", "address", "select_payment", "skip_shipping", "skip_payment", "select_test"]
do: ["@sylius.order_processing.order_processor", "process"]
args: ["object"]
sylius_shop:
product_grid:
include_all_descendants: true
checkout_resolver:
route_map:
cart:
route: sylius_shop_checkout_test
test_selected:
route: sylius_shop_checkout_address
routes/Checkout/checkout.yml:
sylius_shop_checkout_start:
path: /
methods: [GET]
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: sylius_shop_checkout_test
sylius_shop_checkout_test:
path: /test
methods: [GET, PUT]
defaults:
_controller: sylius.controller.order:updateAction
_sylius:
event: test
flash: false
template: "Checkout/Test/test.html.twig"
form:
type: App\Form\TestType
repository:
method: findCartForAddressing
arguments:
- "expr:service('sylius.context.cart').getCart().getId()"
state_machine:
graph: sylius_order_checkout
transition: select_test
#after this all other states as they are in native Sylius in this file. The original checkout.yml is completely overridden with this one.
我在 MY 中遵循了编译器而不是 NATIVE 检出步骤,不同之处在于
Bundle/Controller/ResourceController.php 第 311 行:
$postEventResponse = $postEvent->getResponse();
if (null !== $postEventResponse) {
return $postEventResponse;
}
Response 对象为空(因此 if 未进入,编译器继续生成上述 arbirary/misleading 错误消息)。所以我注意到发生这种情况是因为
symfony/event-dispatcher/EventDispatcher.php 第 72 行:
if ($listeners) {
$this->callListeners($listeners, $eventName, $event);
}
$listeners 在我的例子中是一个空数组,而对于每个其他本地 sylius 步骤,这里至少有 1 个侦听器订阅。我只是不知道在哪里添加它。谁能告诉我 where/how 已经完成了?
我自己解决了。 services.yaml 中缺少的所有内容如下:
app.request.matcher:
class: Symfony\Component\HttpFoundation\RequestMatcher
sylius.listener.checkout_redirect:
class: Sylius\Bundle\CoreBundle\Checkout\CheckoutRedirectListener
tags:
- { name: 'kernel.event_listener', event: 'sylius.order.post_address', method: 'handleCheckoutRedirect'}
- { name: 'kernel.event_listener', event: 'sylius.order.post_select_shipping', method: 'handleCheckoutRedirect'}
- { name: 'kernel.event_listener', event: 'sylius.order.post_payment', method: 'handleCheckoutRedirect'}
- { name: 'kernel.event_listener', event: 'sylius.order.post_test', method: 'handleCheckoutRedirect'}
arguments:
- '@request_stack'
- '@sylius.router.checkout_state'
- '@app.request.matcher'
您可以创建 CompilerPass 并向服务添加标签
<?php
namespace App\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CustomPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container
->getDefinition('sylius.listener.checkout_redirect')
->addTag('kernel.event_listener', [
'event' => 'sylius.order.post_test',
'method' => 'handleCheckoutRedirect',
]);
}
}
我想在 sylius 结帐流程中添加一个结帐步骤。它的工作原理是,当我在购物车概览中并单击结帐时,会显示我添加的步骤。但是,当我单击“下一步”时,出现错误“无法为命名路由“sylius_order_index”生成 URL,因为这样的路由不存在。”
到目前为止我做了什么(现在为了避免分心,我将结帐步骤称为“测试”):
config/packages/_sylius.yaml:
winzou_state_machine:
sylius_order_checkout:
states:
test_selected: ~
transitions:
select_test:
from: [cart, test_selected, addressed, shipping_selected, shipping_skipped, payment_selected, payment_skipped]
to: test_selected
address:
from: [cart, test_selected, addressed, shipping_selected, shipping_skipped, payment_selected, payment_skipped]
to: addressed
select_shipping:
from: [addressed, test_selected, shipping_selected, payment_selected, payment_skipped]
to: shipping_selected
select_payment:
from: [payment_selected, test_selected, shipping_skipped, shipping_selected]
to: payment_selected
callbacks:
after:
sylius_process_cart:
on: ["select_shipping", "address", "select_payment", "skip_shipping", "skip_payment", "select_test"]
do: ["@sylius.order_processing.order_processor", "process"]
args: ["object"]
sylius_shop:
product_grid:
include_all_descendants: true
checkout_resolver:
route_map:
cart:
route: sylius_shop_checkout_test
test_selected:
route: sylius_shop_checkout_address
routes/Checkout/checkout.yml:
sylius_shop_checkout_start:
path: /
methods: [GET]
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: sylius_shop_checkout_test
sylius_shop_checkout_test:
path: /test
methods: [GET, PUT]
defaults:
_controller: sylius.controller.order:updateAction
_sylius:
event: test
flash: false
template: "Checkout/Test/test.html.twig"
form:
type: App\Form\TestType
repository:
method: findCartForAddressing
arguments:
- "expr:service('sylius.context.cart').getCart().getId()"
state_machine:
graph: sylius_order_checkout
transition: select_test
#after this all other states as they are in native Sylius in this file. The original checkout.yml is completely overridden with this one.
我在 MY 中遵循了编译器而不是 NATIVE 检出步骤,不同之处在于
Bundle/Controller/ResourceController.php 第 311 行:
$postEventResponse = $postEvent->getResponse();
if (null !== $postEventResponse) {
return $postEventResponse;
}
Response 对象为空(因此 if 未进入,编译器继续生成上述 arbirary/misleading 错误消息)。所以我注意到发生这种情况是因为
symfony/event-dispatcher/EventDispatcher.php 第 72 行:
if ($listeners) {
$this->callListeners($listeners, $eventName, $event);
}
$listeners 在我的例子中是一个空数组,而对于每个其他本地 sylius 步骤,这里至少有 1 个侦听器订阅。我只是不知道在哪里添加它。谁能告诉我 where/how 已经完成了?
我自己解决了。 services.yaml 中缺少的所有内容如下:
app.request.matcher:
class: Symfony\Component\HttpFoundation\RequestMatcher
sylius.listener.checkout_redirect:
class: Sylius\Bundle\CoreBundle\Checkout\CheckoutRedirectListener
tags:
- { name: 'kernel.event_listener', event: 'sylius.order.post_address', method: 'handleCheckoutRedirect'}
- { name: 'kernel.event_listener', event: 'sylius.order.post_select_shipping', method: 'handleCheckoutRedirect'}
- { name: 'kernel.event_listener', event: 'sylius.order.post_payment', method: 'handleCheckoutRedirect'}
- { name: 'kernel.event_listener', event: 'sylius.order.post_test', method: 'handleCheckoutRedirect'}
arguments:
- '@request_stack'
- '@sylius.router.checkout_state'
- '@app.request.matcher'
您可以创建 CompilerPass 并向服务添加标签
<?php
namespace App\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CustomPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container
->getDefinition('sylius.listener.checkout_redirect')
->addTag('kernel.event_listener', [
'event' => 'sylius.order.post_test',
'method' => 'handleCheckoutRedirect',
]);
}
}