3 minutes October 1, 2024

How to Upload Documents to an Order from a URL in Shopware 6?

As a developer working with Shopware 6, you may often need to integrate external systems seamlessly. One common task is uploading documents, such as invoices, from external URLs to Shopware orders using the platform’s API.

In this article, we will guide you through uploading an invoice (or other documents) to a Shopware 6 order from an external URL using an API. When you need to attach documents—like invoices—that were created or saved on other systems to Shopware orders, this method comes in handy.

Issue: Upload Invoice from Another Site via API

Sometimes, you may need to upload an invoice or document to a Shopware order from an external site. This can be accomplished using an API function to fetch the document from the URL and upload it to Shopware.

Steps to upload documents to order in Shopware 6

Here is the solution for uploading a document to an order in Shopware 6 using the following function: uploadDocuments($orderId, $fileUrl).

Step 1: Fetch the file from the external URL.

The function begins by fetching the file content from the external URL using PHP’s file_get_contents() method.

Step 2: Create a temporary file.

Once the content is fetched, the next step is to create a temporary file in which the fetched content will be stored.

Step 3: Prepare the File Information Object.

Create a file object that includes important file details such as the path, original name, MIME type, and size. This information will be used to pass the file into Shopware’s media helper for upload.

Step 4: Then upload the file to Shopware media.

Using the uploadMedia function from Shopware’s media helper, the file is uploaded to the Shopware media system, returning a mediaId that will be linked to the order document.

After the upload, close the temporary file:

Step 5: Update order with uploaded document.

Now that the document is uploaded, you need to update the Shopware order with this document. This is done by passing the media ID and file type to the updateOrderDocuments() function.

Step 6: Create and associate document with order.

In the updateOrderDocuments() function, the document repository is used to create a new document for the order. The document type is fetched based on the type (e.g., invoice) and the document is linked with the order.

The function getDocumentTypeId() fetches the document type based on the technical name provided (e.g., ‘invoice’):

Full Code Solution

Conclusion

By following this guide, you can seamlessly upload and attach invoices or other documents from external URLs to orders in Shopware 6 via API integration. This allows for automated processing of documents from other systems, improving workflow efficiency. For expert services, contact a professional Shopware agency for smooth implementation.

blog
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
Aneesh ceo
Aneesh Sreedharan
Founder & CEO, 2Hats Logic Solutions
Subscribe to our Newsletter
Aneesh ceo

    Stay In The Loop!

    Subscribe to our newsletter and learn about the latest digital trends.