How to migrate Product Downloads from Shopware 5 to Shopware 6

How to migrate Product Downloads from Shopware 5 to Shopware 6

One of the key challenges encountered during the migration process from Shopware 5 to Shopware 6 is the absence of the product downloads feature in Shopware 6. This gap in migration leaves the process incomplete. In this blog post, we will outline a straightforward step-by-step guide to successfully migrating product downloads from Shopware 5 to Shopware 6.

In Shopware 5, product downloads are stored as part of product media. Therefore, when we utilize the Migration Assistant plugin, all product downloads will be automatically migrated to Shopware 6 media. The remaining steps involve assigning these migrated downloads to the correct products and configuring them for display in the frontend of your Shopware 6 store.

Step 1. Create custom fields for downloads and show them on the product detail page.

We are developing a plugin that includes the creation of custom fields and the extension of product detail page Twig templates. These custom fields will be used to store and display product downloads. Our goal is to showcase these downloads in a new tab near the reviews section. Below, you will find an example of how to add custom field creation code within the class file of the newly created plugin.

public function install(InstallContext $installContext): void
   {


       /**
        * @var EntityRepositoryInterface $customFieldSetRepository
        */


       $customFieldSetRepository = $this->container->get('custom_field_set.repository');


       $customFieldSetRepository->create($this->getCustomFieldSet(), $installContext->getContext());


   }

 

protected function getCustomFieldSet(): array
   {


       return [
           [
               'name' => 'hatslogic_product_downloads',
               'config' => [
                   'label' => [
                       'de-DE' => 'Downloads',
                       'en-GB' => 'Downloads',
                   ],
               ],
               'customFields' => [
                   [
                       'name' => 'hatslogic_product_download_1',
                       'type' => 'media',
                       'config' => [
                           'customFieldPosition' => 1,
                           'componentName' => 'sw-media-field',
                           'customFieldType' => 'media',
                           'label' => [
                               'de-DE' => 'Download 1',
                               'en-GB' => 'Download 1',
                           ],
                       ],
                   ],
               'relations' => [
                   [
                       'entityName' =>  $this->container->get(ProductDefinition::class)->getEntityName()
                   ]
               ]
           ]
       ];


   }

In the services.xml file, please include the following two services:

<service id="HatsLogic\ProductDownloads\Subscriber\PageSubscriber">
           <argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService" />
           <argument type="service" id="HatsLogic\ProductDownloads\Service\ProductDownloadsService" />
           <tag name="kernel.event_subscriber" />
       </service>


       <service id="HatsLogic\ProductDownloads\Service\ProductDownloadsService">
           <argument type="service" id="media.repository"/>
           <argument type="service" id="custom_field.repository"/>
       </service>

 

In the Product Downloads Service class, add a function to get the download file. 

public function getProductDownloads($context, $product)
   {
      
       $mediaFile = [];


       $customFields = $product->getCustomFields();


       if (!empty($customFields)) {


           $mediaId = $customFields['hatslogic_product_download_1'];


           if (!empty($mediaId)) {


               $criteria = new Criteria($mediaId);


               $searchResult = $this->mediaRepository->search($criteria, $context);


               if (!empty($searchResult)) {
                   $mediaFile = $searchResult->getElements();
                   $downloadTitle = $mediaFile->getTitle();


                   if (empty($downloadTitle)) {


                       $downloadTitle = $mediaFile->getFileName();
                   }


                   $mediaFile = [
                       'id' => $mediaFile->getId(),
                       'title' => $downloadTitle,
                       'url' => $mediaFile->getUrl()
                   ];
               }
           }
       }


       return $mediaFile;

 

FAQ

The purpose of creating custom fields for downloads in Shopware 6 during the migration from Shopware 5 is to ensure a smooth transition of product downloads. Shopware 6 does not inherently support product downloads like Shopware 5 does

After migrating from Shopware 5 to Shopware 6, product downloads are stored as part of product media. The Migration Assistant plugin automatically transfers these downloads to Shopware 6 media.

The custom field set plays a crucial role in this migration process. It defines where and how the product downloads will be stored and displayed in Shopware 6. Custom fields are created within this set to accommodate the downloads, ensuring that they are properly organized and accessible

Extending the product detail page templates in Shopware 6 is essential because it enables the display of product downloads in a user-friendly manner. By customizing the templates, you can create a dedicated section for downloads, typically near the reviews section.

Leave a Reply

Your email address will not be published. Required fields are marked *

18 + 11 =

2hats Logic HelpBot