You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发自定义Shopware 6插件时,如何上传修改后的产品图片?

实现Shopware 6自定义插件上传产品图片的方案

核心思路

Shopware 6的媒体上传与产品关联依赖三个核心服务:MediaService(处理文件上传与媒体实体创建)、ProductRepository(更新产品数据)、MediaRepository(辅助媒体实体查询)。你需要通过这几个服务完成「文件上传→创建媒体实体→关联到目标产品」的完整流程。

步骤1:配置服务注入

在插件的src/Resources/config/services.xml中注入所需服务:

<service id="YourPlugin\Controller\ProductImageUploadController" public="true">
    <argument type="service" id="Shopware\Core\Content\Media\MediaService"/>
    <argument type="service" id="Shopware\Core\Content\Product\ProductRepository"/>
    <argument type="service" id="Shopware\Core\Content\Media\MediaRepository"/>
    <argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Search\CriteriaBuilder"/>
    <tag name="controller.service_arguments"/>
</service>

步骤2:编写上传与关联逻辑

在自定义控制器中实现处理上传的方法,以下是简化示例:

<?php

namespace YourPlugin\Controller;

use Shopware\Core\Content\Media\MediaService;
use Shopware\Core\Content\Product\ProductRepository;
use Shopware\Core\Content\Media\MediaRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\CriteriaBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route(defaults: ['_routeScope' => ['api']])]
class ProductImageUploadController extends AbstractController
{
    public function __construct(
        private readonly MediaService $mediaService,
        private readonly ProductRepository $productRepository,
        private readonly MediaRepository $mediaRepository,
        private readonly CriteriaBuilder $criteriaBuilder
    ) {}

    #[Route(path: '/api/_action/your-plugin/product/{productId}/upload-image', name: 'api.action.your-plugin.product.upload-image', methods: ['POST'])]
    public function upload(Request $request, string $productId): Response
    {
        // 1. 获取上传的文件
        $file = $request->files->get('image');
        if (!$file || !$file->isValid()) {
            return new Response('无效文件', Response::HTTP_BAD_REQUEST);
        }

        // 2. 获取默认产品媒体文件夹ID
        $criteria = $this->criteriaBuilder->create(['name' => 'product']);
        $folder = $this->mediaRepository->search($criteria, $this->get('Shopware\Core\Framework\Context')->createDefaultContext())->first();
        if (!$folder) {
            return new Response('未找到产品媒体文件夹', Response::HTTP_INTERNAL_SERVER_ERROR);
        }

        // 3. 创建媒体实体
        $mediaId = $this->mediaService->upload(
            $file->getPathname(),
            $file->getClientOriginalName(),
            $folder->getId(),
            $this->get('Shopware\Core\Framework\Context')->createDefaultContext()
        );

        // 4. 关联媒体到产品(设置为主图,排序位1)
        $this->productRepository->update([
            [
                'id' => $productId,
                'media' => [
                    [
                        'mediaId' => $mediaId,
                        'position' => 1,
                        'main' => true
                    ]
                ]
            ]
        ], $this->get('Shopware\Core\Framework\Context')->createDefaultContext());

        return new Response('图片上传并关联成功', Response::HTTP_OK);
    }
}

步骤3:权限与额外处理

  • 确保插件声明必要权限,在src/Resources/config/permissions.xml中添加:
<permissions>
    <permission>media.create</permission>
    <permission>product.edit</permission>
</permissions>
  • 可自行添加文件格式/大小验证,比如限制仅允许jpg/png格式;
  • 如果需要替换产品现有图片,可先查询产品的现有媒体关联,删除或替换对应条目;
  • Shopware会自动生成媒体缩略图,无需额外处理。

内容的提问来源于stack exchange,提问作者Templator Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 09:01:35