Add Image to the product object

Hy ,
how to add an image to the product object ?I see that there is an addImage method inSylius\Component\Core\Model\Product , but I can not use it

public function addImage(ImageInterface $image): void
{
    $image->setOwner($this);
    $this->images->add($image);
}

thanks for help

Hi, how did you try to add an object?

you can do this, for example:

    $uploadedImage = new UploadedFile($imagePath, basename($imagePath));

    /** @var ImageInterface $productImage */
    $productImage = $this->productImageFactory->createNew();
    $productImage->setFile($uploadedImage);
    $productImage->setType(end($image) ?: null);

    $this->imageUploader->upload($productImage);

    $product->addImage($productImage);
1 Like

^ this should work! :slight_smile:

1 Like

thanks you , its clear now

How do I acquire the productImageFactory class?

you can inject a service sylius.factory.product_image

1 Like

Thank you!:+1: that worked