John Davidson

php - Shopware 6 how create a product with media with the Admin API?

0 comments
Message:


How can i create a product with media with the Shopware 6 Admin API PHP SDK ?


Shopware 6 Admin API PHP SDK


I know how to create a product with following snippet.


$productRepository->create([
'id' => Uuid::randomHex(),
'name' => 'New Product',
'taxId' => $product->taxId,
'price' => $product->price,
'productNumber' => $product->productNumber . random_int(10, 1000),
'stock' => $product->stock,
'media' => $product->media, //Not working
], $context);

And how to upload media from a URL with following snippet.


$mediaService->uploadMediaFromUrl($mediaId, $url, 'jpg', 'test-media');

But how can i add the media to the product ?



My Answer:
>create([
[
'name' => 'Product Name',
'productNumber' => '12345',
'price' => [
['currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca', 'gross' => 19.99, 'net' => 16.79, 'linked' => false],
],
'manufacturer' => ['name' => 'Manufacturer Name'],
'tax' => ['name' => '19%'],
'active' => true,
]
]);

To create a product with media using the Shopware 6 Admin API PHP SDK, you can use the following code snippet:

php
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\SearchRequestException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\SearchFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\TermFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\Sorting;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\SortingCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
use Shopware\Core\Framework\DataAbstractionLayer\Write\EntityExistence;
use Shopware\Core\Framework\DataAbstractionLayer\Write\EntityWriterInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteException;
use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteResult;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\Media\MediaEntity;

class ProductMediaCreator
{
private EntityRepositoryInterface $productRepository;
private EntityRepositoryInterface $mediaRepository;
private EntityWriterInterface $entityWriter;

public function __construct(
EntityRepositoryInterface $productRepository,
EntityRepositoryInterface $mediaRepository,
EntityWriterInterface $entityWriter
) {
$this->productRepository = $productRepository;
$this->mediaRepository = $mediaRepository;
$this->entityWriter = $entityWriter;
}

public function createProductWithMedia(array $productData, array $mediaData, Context $context): WriteResult
{
$productId = Uuid::randomHex();
$mediaId = Uuid::randomHex();

$productData['id'] = $productId;
$mediaData['id'] = $mediaId;

$productData['mediaId'] = $mediaId;

$this->entityWriter->upsert(
$this->productRepository->getDefinition(),
[
new InsertCommand($productData, $context, EntityExistence::EXISTING),
],
$context
);

$this->entityWriter->upsert(
$this->mediaRepository->getDefinition(),
[
new InsertCommand($mediaData, $context, EntityExistence::EXISTING),
],
$context
);

return new WriteResult([$productId, $mediaId], [], $context);
}
}

// Usage
$productMediaCreator = new ProductMediaCreator($productRepository, $mediaRepository, $entityWriter);

$productData = [
'name' => 'Product Name',
'productNumber' => '12345',
'price' => [
['currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca', 'gross' => 19.99, 'net' => 16.79, 'linked' => false],
],
'manufacturer' => ['name' => 'Manufacturer Name'],
'tax' => ['name' => '19%'],
'active' => true,
];

$mediaData = [
'fileName' => 'example.jpg',
'fileSize' => 12345,
'mimeType' => 'image/jpeg',
'mediaType' => 'IMAGE',
'url' => 'https://example.com/example.jpg',
];

$productMediaCreator->createProductWithMedia($productData, $mediaData, $context);


In this code snippet, we create a `ProductMediaCreator` class that takes the necessary repositories and entity writer as dependencies. The `createProductWithMedia` method creates a product and a media entity, links them together, and upserts them using the entity writer. You can then use this class to create a product with media in your Shopware 6 application.

Rate this post

4 of 5 based on 2015 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us