Reputation: 181
I am working on custom CLI Product Importer plugin for Shopware, and I am kind of stuck on media part. So far I have CSV Importer, which works perfectly. I can import all products as well I created Media Importer, that uploads all images to product media directory. But the thing is, that I don’t know how to make the „bridge“, connection between media repository and product repository to be able to set cover and media(images) for a product
To sum up, my question is, how can I set the cover id? As far as I know, ID comes from product_media table?
Product Mapper
{
"product": {
"productNumber": "Product Number",
"name": "Name",
"active": {
"key": "Active",
"type": "bool"
},
"media": {
"map": {
"fileName": "Media"
}
},
"cover": {
"map": {
"mediaId": "Cover"
}
},
"description": "Description",
"stock": {
"key": "Stock",
"type": "int"
},
ProductService
/**
* @throws Throwable
*/
public function setProducts(array $rows, Context $context): void
{
foreach ($rows as $row) {
$this->setProduct($row, $context);
}
}
public function setProduct(array $row, Context $context): void
{
try {
$productId = $this->getId('productNumber', $row['productNumber'], $context);
if ($productId) {
$row['id'] = $productId;
$this->update($row, $context);
} else {
$this->create($row, $context);
}
} catch (Throwable $throwable) {
throw $throwable;
}
}
Upvotes: 0
Views: 1181
Reputation: 13201
cover
is a ManyToOneAssociationField
and has a local foreign key with the property name coverId
you can set directly. The id should be one of a product_media
entity.
$this->productRepository->update(
[
[
'id' => $productId,
'coverId' => $productMediaId,
'media' => [
[
'id' => $productMediaId,
],
],
],
],
$context
);
Upvotes: 2