Add Image to the product object


#1

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


#2

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);

#3

^ this should work! :slight_smile:


#4

thanks you , its clear now


#5

How do I acquire the productImageFactory class?


#6

you can inject a service sylius.factory.product_image


#7

Thank you!:+1: that worked