通过时事通讯代码获取有关 shopware 6 的电子邮件。(提交后的自定义操作)
Get email on shopware 6 by newsletter's code.(custom action after submit)
为了在 shopware 6 中获取用户的电子邮件。我像上面那样使用了时事通讯的代码
<div class="cms-block footer-newsletter w-100">
<div class="cms-element-form ">
<form action="{{ path('frontend.form.newsletter.register.handle') }}"
method="post"
class="d-flex nws-search py-5"
data-form-csrf-handler="true"
data-form-validation="true">
{{ sw_csrf('frontend.form.newsletter.register.handle') }}
{% set formViolations = app.request.get('errors') %}
<input type="hidden" name="option" value="subscribe"/>
<input type="submit" class="submit--hidden d-none">
<input name="email"
type="email"
id="footerNewsletterMail"
placeholder="{{ "account.personalMailPlaceholder"|trans }}{{ "general.required"|trans }}"
required="required"
value="{{ data.get('email') }}"
class="px-3 py-2 py-sm-3 {% if formViolations.getViolations('/email') %} is-invalid{% endif %}"/>
<button type="submit" class="px-4 py-2 py-sm-3">JETZT REGISTERIEREN</button>
</form>
</div>
</div>
提交Shopware后默认为work.But隐藏Form然后显示在面板信息中。
我不想隐藏表单。我希望表单信息面板显示在我的表单上方 newsletter.How 我可以做到吗?
为了解决我discued.You必须定义一个Controller的问题。
创建一个像上面的控制器 src/Controller/NewsletterController.php
<?php declare(strict_types=1);
namespace AK\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\Content\ContactForm\ContactFormService;
use Shopware\Core\Content\Newsletter\NewsletterSubscriptionServiceInterface;
use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Storefront\Framework\Captcha\Annotation\Captcha;
/**
* @RouteScope(scopes={"storefront"})
*/
class NewsletterController extends StorefrontController
{
/**
* @var NewsletterSubscriptionServiceInterface
*/
private $newsletterService;
public function __construct(
ContactFormService $contactFormService,
NewsletterSubscriptionServiceInterface $newsletterService
) {
$this->contactFormService = $contactFormService;
$this->newsletterService = $newsletterService;
}
/**
* @Route("/form/newsletter/custom", name="frontend.form.newsletter.register.handle", methods={"POST"}, defaults={"XmlHttpRequest"=true})
* @Captcha
*/
public function handleNewsletter(RequestDataBag $data, SalesChannelContext $context): JsonResponse
{
$response = $this->handleSubscribe($data, $context);
return new JsonResponse($response);
}
private function handleSubscribe(RequestDataBag $data, SalesChannelContext $context): array
{
try {
$this->newsletterService->subscribe($data, $context);
$response[] = [
'type' => 'success',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'info',
'list' => [$this->trans('newsletter.subscriptionPersistedInfo')],
])
];
$response[] = [
'type' => 'info',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'info',
'list' => [$this->trans('newsletter.subscriptionPersistedInfo')],
]),
];
} catch (ConstraintViolationException $exception) {
$errors = [];
foreach ($exception->getViolations() as $error) {
$errors[] = $error->getMessage();
}
$response[] = [
'type' => 'danger',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'danger',
'list' => $errors,
]),
];
} catch (\Exception $exception) {
$response[] = [
'type' => 'danger',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'danger',
'list' => [$this->trans('error.message-default')],
]),
];
}
return $response;
}
}
然后在 config/servives 中定义您的控制器。xml
<services>
<service id="AK\Controller\NewsletterController" public="true">
<argument type="service" id="Shopware\Core\Content\ContactForm\ContactFormService" />
<argument type="service" id="Shopware\Core\Content\Newsletter\NewsletterSubscriptionService"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
</services>
</container>
并在config/routs.xml
中定义
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<import resource="../../Controller" type="annotation" />
</routes>
现在您可以通过控制器管理表单中的响应操作。
为了在 shopware 6 中获取用户的电子邮件。我像上面那样使用了时事通讯的代码
<div class="cms-block footer-newsletter w-100">
<div class="cms-element-form ">
<form action="{{ path('frontend.form.newsletter.register.handle') }}"
method="post"
class="d-flex nws-search py-5"
data-form-csrf-handler="true"
data-form-validation="true">
{{ sw_csrf('frontend.form.newsletter.register.handle') }}
{% set formViolations = app.request.get('errors') %}
<input type="hidden" name="option" value="subscribe"/>
<input type="submit" class="submit--hidden d-none">
<input name="email"
type="email"
id="footerNewsletterMail"
placeholder="{{ "account.personalMailPlaceholder"|trans }}{{ "general.required"|trans }}"
required="required"
value="{{ data.get('email') }}"
class="px-3 py-2 py-sm-3 {% if formViolations.getViolations('/email') %} is-invalid{% endif %}"/>
<button type="submit" class="px-4 py-2 py-sm-3">JETZT REGISTERIEREN</button>
</form>
</div>
</div>
提交Shopware后默认为work.But隐藏Form然后显示在面板信息中。 我不想隐藏表单。我希望表单信息面板显示在我的表单上方 newsletter.How 我可以做到吗?
为了解决我discued.You必须定义一个Controller的问题。 创建一个像上面的控制器 src/Controller/NewsletterController.php
<?php declare(strict_types=1);
namespace AK\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\Content\ContactForm\ContactFormService;
use Shopware\Core\Content\Newsletter\NewsletterSubscriptionServiceInterface;
use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Storefront\Framework\Captcha\Annotation\Captcha;
/**
* @RouteScope(scopes={"storefront"})
*/
class NewsletterController extends StorefrontController
{
/**
* @var NewsletterSubscriptionServiceInterface
*/
private $newsletterService;
public function __construct(
ContactFormService $contactFormService,
NewsletterSubscriptionServiceInterface $newsletterService
) {
$this->contactFormService = $contactFormService;
$this->newsletterService = $newsletterService;
}
/**
* @Route("/form/newsletter/custom", name="frontend.form.newsletter.register.handle", methods={"POST"}, defaults={"XmlHttpRequest"=true})
* @Captcha
*/
public function handleNewsletter(RequestDataBag $data, SalesChannelContext $context): JsonResponse
{
$response = $this->handleSubscribe($data, $context);
return new JsonResponse($response);
}
private function handleSubscribe(RequestDataBag $data, SalesChannelContext $context): array
{
try {
$this->newsletterService->subscribe($data, $context);
$response[] = [
'type' => 'success',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'info',
'list' => [$this->trans('newsletter.subscriptionPersistedInfo')],
])
];
$response[] = [
'type' => 'info',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'info',
'list' => [$this->trans('newsletter.subscriptionPersistedInfo')],
]),
];
} catch (ConstraintViolationException $exception) {
$errors = [];
foreach ($exception->getViolations() as $error) {
$errors[] = $error->getMessage();
}
$response[] = [
'type' => 'danger',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'danger',
'list' => $errors,
]),
];
} catch (\Exception $exception) {
$response[] = [
'type' => 'danger',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'danger',
'list' => [$this->trans('error.message-default')],
]),
];
}
return $response;
}
}
然后在 config/servives 中定义您的控制器。xml
<services>
<service id="AK\Controller\NewsletterController" public="true">
<argument type="service" id="Shopware\Core\Content\ContactForm\ContactFormService" />
<argument type="service" id="Shopware\Core\Content\Newsletter\NewsletterSubscriptionService"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
</services>
</container>
并在config/routs.xml
中定义<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<import resource="../../Controller" type="annotation" />
</routes>
现在您可以通过控制器管理表单中的响应操作。