1

I'm trying to create a web service rest to add an image to category, but I can't find the Model method that allow to do this.

I'm looking for something like

addImage of Magento\Catalog\Model\Product\Gallery\Processor

for the category.

Thanks

1 Answer 1

1

For category, by default, seem that we cannot do the same, however, we can add the image like this:

/** @var \Magento\Catalog\Model\Category $category **/

$category->isObjectNew(true);
$category->setId(5)
    ->setName('Category 1.1.1')
    ->setParentId(4)
    ->setPath('1/2/3/4/5')
    ->setLevel(4)
    ->setAvailableSortBy('name')
    ->setDefaultSortBy('name')
    ->setIsActive(true)
    ->setPosition(1)
    ->setCustomUseParentSettings(0)
    ->setCustomDesign('Magento/blank')
    ->setThumbnail('media/catalog/category/test.jpg')
    ->save();

With API (not yet test)

/** @var \Magento\Catalog\Api\CategoryRepositoryInterface $category **/

$category->get($categoryId)
         ->setThumbnail('media/catalog/category/test.jpg');
         ->save();
5
  • Thanks for the answer @Khoa, the path on setThumbnail method is yuor local path or the image have to be uploaded before on magento? Commented Sep 15, 2016 at 7:23
  • The image have to be uploaded before on Magento. Commented Sep 15, 2016 at 7:26
  • This is a problem, because I'm creating a webservice to allow third part to upload the images and data of categories. So the method need also to upload the image from custom path Commented Sep 15, 2016 at 7:30
  • 1
    I got the soluction, for categories to upload images it's only necessary put them in the right folder and set the name on image attribute. Commented Sep 15, 2016 at 8:20
  • Yeah! You can edit my answer or answer your own question. Commented Sep 15, 2016 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.