2

How can I make an image of a product the base image ? I tought setting the position would be enough but that's not true.

I create my products via the REST api , this works fine.

The images are uploaded correct.

But I don't know how to tag an image as the base image. When I have multiple images it is always the last images that is tagged as base automaticly, I don't want that. How could I set that in my array?

$product = [
 "sku" => str_replace(' ', '-', $data['name']),
 "name" => $data['name'],
 "type_id" => 'configurable',
 "price" => 0,
 'attribute_set_id' => 9, // default
 "extension_attributes" => [],
 "custom_attributes" => [],
 'media_gallery_entries' => $this->getMediaGalleryEntries($data)
];
function getMediaGalleryEntries($data)
{
 $image = 'path/to/my/image';
 $entries[] = [
 'position' => $iterator,
 'media_type' => 'image',
 'disabled' => false,
 'label' => '',
 'types' => ['image', 'small_image', 'thumbnail'],
 'content' => [
 'type' => 'image/jpeg',
 'name' => pathinfo($image,PATHINFO_FILENAME).'.'.pathinfo($image, PATHINFO_EXTENSION),
 'base64_encoded_data' => base64_encode(file_get_contents($image)),
 ]
 ];
 return $entries;
}
Rakesh Jesadiya
42.5k19 gold badges135 silver badges186 bronze badges
asked May 17, 2016 at 15:35

2 Answers 2

1

I've solved it like this:

 foreach($images as $image) {
 $entries[] = [
 'position' => $iterator,
 'media_type' => 'image',
 'disabled' => false,
 'label' => '',
 'types' => ($iterator == 1 ? ['image','small_image','thumbnail'] : []),
 'content' => [
 'type' => 'image/jpeg',
 'name' => pathinfo($image,PATHINFO_FILENAME).'.'.pathinfo($image, PATHINFO_EXTENSION),
 'base64_encoded_data' => base64_encode(file_get_contents($image)),
 ]
 ];
 $iterator++;
}

Types should only be assigned to the first image.This is the image I want as base image.

answered May 18, 2016 at 7:47
1
  • Did you come across a problem where saving the product with the image assigned to it causes this error in the admin: "Item (Magento\Framework\DataObject) with the same ID "1" already exists." ? I am trying to find out why this is. Commented Aug 21, 2016 at 23:41
0

In Magento1, you would have set the image attribute on the product object to the image you have created in a media object.

Looking at your Import Data it seems to be the types array in the gallery object which defines what the image is used for (image, small_image or thumbnail) while image should be the base image.

This also explains why always the last image is the base image when you import all images with all three types

answered May 17, 2016 at 15:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.