如何在 Shopware 6 中通过 twig 将产品 URL 添加到电子邮件?

发布于 2025-01-14 17:07:56 字数 278 浏览 4 评论 0原文

我想通过电子邮件发送产品链接。

我尝试在我的电子邮件树枝中使用 {{ seoUrl('frontend.detail.page', { 'productId': Product.id }) }} 生成 URL。但链接的格式似乎不正确。缺少域,而是显示域占位符。

结果如下所示: 124c71d524604ccbad6042edce3ac799/detail/47a05ce596394f95943f6e77b652dcab#

如何正确设置链接格式?

I would like to send links to products in an E-Mail.

I tried to generate URLs using {{ seoUrl('frontend.detail.page', { 'productId': product.id }) }} in my E-Mail twig. But the links dont seem to be formatted correctly. The domain is missing and the domain placeholder is being displayed instead.

The result is looking like this: 124c71d524604ccbad6042edce3ac799/detail/47a05ce596394f95943f6e77b652dcab#

How can I correctly format my links?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大海や 2025-01-21 17:07:56

使用 rawUrl 函数应该可以。

{{ rawUrl('frontend.detail.page', { 'productId': product.id }, salesChannel.domain|first.url) }}

Using the rawUrl function should work.

{{ rawUrl('frontend.detail.page', { 'productId': product.id }, salesChannel.domain|first.url) }}
牵你手 2025-01-21 17:07:56

我知道,为时已晚,但希望它能帮助将来的人...

正如 @Alex 正确提到的那样,不会调用 \Shopware\Core\Content\Seo\SeoUrlPlaceholderHandler::replace 方法在你的场景中。它仅从 StorefrontController::renderStorefront() 方法调用,因此要获得漂亮的 seo url,您需要使用 StorefrontController 扩展控制器并返回 $this-> ;renderStorefront(...):

namespace App\Bundle\Controller;

use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\Routing\Annotation\Route;

#[Route(defaults: ['_routeScope' => ['storefront']])]
class BundleController extends StorefrontController
{
#[Route('/page-with-urls', name: 'url-list', methods: ['GET'])]
    public function index()
    {
        // your logic is here

        return $this->renderStorefront(...);
    }
}

没有 StorefrontController 的替代解决方案

如果由于某种原因您无法使用 StorefrontController,您可以只使用所需的功能StorefrontController::renderStorefront()

namespace App\Bundle\Controller;

use Shopware\Storefront\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

#[Route(defaults: ['_routeScope' => ['storefront']])]
class BundleController extends AbstractController
{
#[Route('/page-with-urls', name: 'url-list', methods: ['GET'])]
    public function index(Request $request, SalesChannelContext $context, SeoUrlPlaceholderHandlerInterface $seoUrlReplacer)
    {
        // your logic is here

        $response =  $this->render(...);

        $content = $response->getContent();
        $host = $request->attributes->get(RequestTransformer::STOREFRONT_URL);

        if ($content !== false) {
            $response->setContent(
                $seoUrlReplacer->replace($content, $host, $context)
            );
        }

        return $response;
    }
}

I know, it is too late, but hope it will help someone in future...

As @Alex correctly mentioned, the \Shopware\Core\Content\Seo\SeoUrlPlaceholderHandler::replace method is not called in your scenario. It is called only from the StorefrontController::renderStorefront() method, so to get nice seo urls you need extend your controller with StorefrontController and return $this->renderStorefront(...):

namespace App\Bundle\Controller;

use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\Routing\Annotation\Route;

#[Route(defaults: ['_routeScope' => ['storefront']])]
class BundleController extends StorefrontController
{
#[Route('/page-with-urls', name: 'url-list', methods: ['GET'])]
    public function index()
    {
        // your logic is here

        return $this->renderStorefront(...);
    }
}

Alternative solution without StorefrontController

If for some reason you cannot use StorefrontController, you may just use the needed functionality from StorefrontController::renderStorefront():

namespace App\Bundle\Controller;

use Shopware\Storefront\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

#[Route(defaults: ['_routeScope' => ['storefront']])]
class BundleController extends AbstractController
{
#[Route('/page-with-urls', name: 'url-list', methods: ['GET'])]
    public function index(Request $request, SalesChannelContext $context, SeoUrlPlaceholderHandlerInterface $seoUrlReplacer)
    {
        // your logic is here

        $response =  $this->render(...);

        $content = $response->getContent();
        $host = $request->attributes->get(RequestTransformer::STOREFRONT_URL);

        if ($content !== false) {
            $response->setContent(
                $seoUrlReplacer->replace($content, $host, $context)
            );
        }

        return $response;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文