Skip to content

SEO Url Support for E-Mail Templates #37

@AlexBachmann

Description

@AlexBachmann

Please describe the feature you would like to see implemented.

Hey there,

great plugin. We've implemented something similar and we recently added support for SEO URLs within the mail template.
It's a great addition, if you want to link to products, categories, etc.

   /**
     * @throws StringTemplateRenderingException
     */
    public function render(string $templateSource, array $data, Context $context): string
    {
        $name = \md5($templateSource);
        $this->twig->setLoader(new ChainLoader([$this->platformTwig->getLoader(), new ArrayLoader([$name => $templateSource])]));

        $this->twig->addGlobal('context', $context);

        try {
            $content = $this->twig->render($name, $data);
            $request = $this->requestStack->getCurrentRequest();
            if (!$request) {
                return $content;
            }
            $host = $request->attributes->get(RequestTransformer::STOREFRONT_URL);
            if (!$host) {
                $host = $request->getSchemeAndHttpHost();
            }
            if (!$host) {
                return $content;
            }
            $criteria = new Criteria();
            $criteria->addFilter(new EqualsFilter('domains.url', $host));
            /** @var SalesChannelEntity $salesChannel */
            $salesChannel = $this->salesChannelRepo->search($criteria, $context)->first();
            if (!$salesChannel) {
                return $content;
            }
            $salesChannelContext = $this->salesChannelContextFactory->create(Uuid::randomHex(), $salesChannel->getId());
            return $this->seoUrlPlaceholderHandler->replace($content, $host, $salesChannelContext);
        } catch (Error $error) {
            throw new StringTemplateRenderingException($error->getMessage());
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions