Shopware 6 – 如何将产品或产品列表添加到自定义插件?

huangapple go评论61阅读模式
英文:

Shopware 6 - How to add a product or list of products to custom plugin?

问题

I'm really new to Shopware 6 and I'm learning it through videos and documentations.
我对Shopware 6真的很新,并通过视频和文档学习它。

I created a custom plugin, added a controller, service, etc... and created a page with it (As we learn the basics).
我创建了一个自定义插件,添加了控制器、服务等... 并使用它创建了一个页面(当我们学习基础知识时)。

I searched everywhere on how to pass a product or list of products to the custom plugin and render it to the page, does anyone know how to do it? Which steps I should take to be able to do it?
我到处搜索了如何将产品或产品列表传递给自定义插件并将其呈现在页面上,有人知道如何做吗?我应该采取哪些步骤才能实现这一目标?

Really appreciate any help Shopware 6 – 如何将产品或产品列表添加到自定义插件?
非常感谢任何帮助 Shopware 6 – 如何将产品或产品列表添加到自定义插件?

Sorry if I misspelled something, my English is not perfect.
如果我拼错了什么,请原谅,我的英语不完美。

英文:

I'm really new to Shopware 6 and I'm learning it through videos and documentations.
I created a custom plugin, added a controller, service, etc... and created a page with it (As we learn the basics).
I searched everywhere on how to pass a product or list of products to the custom plugin and render it to the page, does anyone know how to do it? Which steps I should take to be able to do it?

Really appreciate any help Shopware 6 – 如何将产品或产品列表添加到自定义插件?

  • Sorry if I misspelled something, my English is not perfect.

答案1

得分: 2

老实说,这个问题太过于笼统,实际上很难回答。这里可能要引用一大半的文档内容。如果没有具体的细节或者在实施过程中遇到了什么问题,我只能提供最基本的示例。

你通过在控制器的服务定义中引用产品存储库来注入它:

<service id="MyPlugin\Controller\MyCustomController">
    <argument type="service" id="sales_channel.product.repository"/>

    <call method="setContainer">
        <argument type="service" id="service_container"/>
    </call>
    <call method="setTwig">
        <argument type="service" id="twig"/>
    </call>
</service>

在路由的方法中,你使用注入的存储库获取产品并将其传递到模板:

class MyCustomController extends StorefrontController
{
    private SalesChannelRepositoryInterface $productRepository;

    public function __construct(
        SalesChannelRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * @Route("/custom/route", name="my-plugin.frontend.custom-route", methods={"GET"})
     */
    public function customPage(SalesChannelContext $context): Response
    {
        $criteria = new Criteria();

        // 处理 criteria,添加过滤器等...

        $products = $this->productRepository->search($criteria, $context);

        return $this->renderStorefront('@MyPlugin/storefront/custom/route/index.html.twig', ['products' => $products]);
    }
}

然后在你的模板中,你可以迭代产品。

{# storefront/custom/route/index.html.twig #}

{% for product in products %}
    {{ product.name }}
{% endfor %}
英文:

Honestly, this question is way too general to really be answered. One could quote half of the documentation here. Without knowing specific details or what your problems during implementation may be, I really can only offer the most basic example.

You inject the product repository by referencing it in the service definition of your controller:

<service id="MyPlugin\Controller\MyCustomController">
    <argument type="service" id="sales_channel.product.repository"/>

    <call method="setContainer">
        <argument type="service" id="service_container"/>
    </call>
    <call method="setTwig">
        <argument type="service" id="twig"/>
    </call>
</service>

In the method of your route you use the injected repository to fetch the products and pass it to your template:

class MyCustomController extends StorefrontController
{
    private SalesChannelRepositoryInterface $productRepository;

    public function __construct(
        SalesChannelRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * @Route("/custom/route", name="my-plugin.frontend.custom-route", methods={"GET"})
     */
    public function customPage(SalesChannelContext $context): Response
    {
        $criteria = new Criteria();

        // do something with the criteria, add filters etc...

        $products = $this->productRepository->search($criteria, $context);

        return $this->renderStorefront('@MyPlugin/storefront/custom/route/index.html.twig', ['products' => $products]);
    }
}

Then in your template you can iterate the products.

{# storefront/custom/route/index.html.twig #}

{% for product in products %}
    {{ product.name }}
{% endfor %}

huangapple
  • 本文由 发表于 2023年3月31日 21:09:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定