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;
}
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.

  • 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. – GuruBob Aug 21 '16 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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