我假设您想在客户下订单时将自定义字段保存在结帐页面上。
shipping-method.html.twig
通过创建一个新的模板文件来扩展模板src/Resources/views/storefront/component/shipping/shipping-method.html.twig
:
{% sw_extends '@Storefront/storefront/component/shipping/shipping-method.html.twig' %}
{% block extended_block_name %}
{{ parent() }}
<input type="text" name="customParcelLockerField" form="confirmOrderForm">
{% endblock %}
Shopware 不会自动保存此字段。您必须使用订阅者自己执行此操作。例如,执行此操作的好地方是CartConvertedEvent
. 当客户的购物车转换为订单时发送。
创建一个src/Subscriber/CartConvertedSubscriber.php
包含您的订阅者类的新文件:
<?php declare(strict_types=1);
namespace YourNamespace\Subscriber;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CartConvertedSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'addCustomFieldsToOrder'
];
}
public function addCustomFieldsToOrder(CartConvertedEvent $event): void
{
$orderData = $event->getConvertedCart();
$orderCustomFields = $orderData['customFields'] ?? [];
$customParcelLocker = $this->requestStack->getCurrentRequest()->request->get('customParcelLockerField');
if ($customParcelLocker) {
$orderCustomFields['custom_parcel_locker_field'] = $customParcelLocker;
}
$orderData['customFields'] = $orderCustomFields;
$event->setConvertedCart($orderData);
}
}
然后在以下位置注册您的订阅者src/Resources/config/services.xml
:
<service id="YourNamespace\Subscriber\CartConvertedSubscriber">
<argument type="service" id="request_stack"/>
<tag name="kernel.event_subscriber"/>
</service>