Imported image roles are lost on update in Magento 2












2














I use an import script to create products with associated images that are assigned to roles thumbnail and small image.



It works well, products and images are imported and they display as thumbnail and small image on the front-end website.



But when I try to edit one of those products via Magento admin, the roles thumbnail and small image are not selected in the UI and saving the product will make this image's roles to be lost.



Why may cause effective thumbnail to not be detected as one when loading the Edit product page? Did I do something the bad way in this import script?



Any clue of what the source of this problem can be?



Here is the import code:



    $product->setMediaGallery(array(
'images' => array(),
'values' => array()
));

foreach ($product['pictures'] as $key => $picture)
{
$image_url = $picture['picture'];
$image_ext = pathinfo($image_url, PATHINFO_EXTENSION);
$imageData = base64_encode(file_get_contents($image_url));
$image = $this->_imageContentFactory->create()
->setType('image/jpeg')
->setName($sku . '-' . $key . '.' . $image_ext)
->setBase64EncodedData($imageData);

$entry = $this->_productAttributeMediaGalleryEntryFactory->create();
$entry->setContent($image);
$entry->setMediaType('image');
if (0 == $key)
$entry->setTypes(['image', 'small_image', 'thumbnail']);
else
$entry->setTypes(['image']);

$entry->setDisabled(false);
$entry->setPosition($key);

$entries = $entry;
}

$product->setMediaGalleryEntries($entries);

$product->setImage($entries[0]->getFile());
$product->setThumbnail($entries[0]->getFile());
$product->setSmallImage($entries[0]->getFile());
$this->_productRepository->save($product);


EDIT: after more investigation, it appears roles are only lost for 'all store views', in any other store views, the image rules are correctly detected.










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • I am facing this exact issue! Only difference is we are using default Magento import
    – Hashid
    Apr 7 '18 at 6:56
















2














I use an import script to create products with associated images that are assigned to roles thumbnail and small image.



It works well, products and images are imported and they display as thumbnail and small image on the front-end website.



But when I try to edit one of those products via Magento admin, the roles thumbnail and small image are not selected in the UI and saving the product will make this image's roles to be lost.



Why may cause effective thumbnail to not be detected as one when loading the Edit product page? Did I do something the bad way in this import script?



Any clue of what the source of this problem can be?



Here is the import code:



    $product->setMediaGallery(array(
'images' => array(),
'values' => array()
));

foreach ($product['pictures'] as $key => $picture)
{
$image_url = $picture['picture'];
$image_ext = pathinfo($image_url, PATHINFO_EXTENSION);
$imageData = base64_encode(file_get_contents($image_url));
$image = $this->_imageContentFactory->create()
->setType('image/jpeg')
->setName($sku . '-' . $key . '.' . $image_ext)
->setBase64EncodedData($imageData);

$entry = $this->_productAttributeMediaGalleryEntryFactory->create();
$entry->setContent($image);
$entry->setMediaType('image');
if (0 == $key)
$entry->setTypes(['image', 'small_image', 'thumbnail']);
else
$entry->setTypes(['image']);

$entry->setDisabled(false);
$entry->setPosition($key);

$entries = $entry;
}

$product->setMediaGalleryEntries($entries);

$product->setImage($entries[0]->getFile());
$product->setThumbnail($entries[0]->getFile());
$product->setSmallImage($entries[0]->getFile());
$this->_productRepository->save($product);


EDIT: after more investigation, it appears roles are only lost for 'all store views', in any other store views, the image rules are correctly detected.










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • I am facing this exact issue! Only difference is we are using default Magento import
    – Hashid
    Apr 7 '18 at 6:56














2












2








2







I use an import script to create products with associated images that are assigned to roles thumbnail and small image.



It works well, products and images are imported and they display as thumbnail and small image on the front-end website.



But when I try to edit one of those products via Magento admin, the roles thumbnail and small image are not selected in the UI and saving the product will make this image's roles to be lost.



Why may cause effective thumbnail to not be detected as one when loading the Edit product page? Did I do something the bad way in this import script?



Any clue of what the source of this problem can be?



Here is the import code:



    $product->setMediaGallery(array(
'images' => array(),
'values' => array()
));

foreach ($product['pictures'] as $key => $picture)
{
$image_url = $picture['picture'];
$image_ext = pathinfo($image_url, PATHINFO_EXTENSION);
$imageData = base64_encode(file_get_contents($image_url));
$image = $this->_imageContentFactory->create()
->setType('image/jpeg')
->setName($sku . '-' . $key . '.' . $image_ext)
->setBase64EncodedData($imageData);

$entry = $this->_productAttributeMediaGalleryEntryFactory->create();
$entry->setContent($image);
$entry->setMediaType('image');
if (0 == $key)
$entry->setTypes(['image', 'small_image', 'thumbnail']);
else
$entry->setTypes(['image']);

$entry->setDisabled(false);
$entry->setPosition($key);

$entries = $entry;
}

$product->setMediaGalleryEntries($entries);

$product->setImage($entries[0]->getFile());
$product->setThumbnail($entries[0]->getFile());
$product->setSmallImage($entries[0]->getFile());
$this->_productRepository->save($product);


EDIT: after more investigation, it appears roles are only lost for 'all store views', in any other store views, the image rules are correctly detected.










share|improve this question















I use an import script to create products with associated images that are assigned to roles thumbnail and small image.



It works well, products and images are imported and they display as thumbnail and small image on the front-end website.



But when I try to edit one of those products via Magento admin, the roles thumbnail and small image are not selected in the UI and saving the product will make this image's roles to be lost.



Why may cause effective thumbnail to not be detected as one when loading the Edit product page? Did I do something the bad way in this import script?



Any clue of what the source of this problem can be?



Here is the import code:



    $product->setMediaGallery(array(
'images' => array(),
'values' => array()
));

foreach ($product['pictures'] as $key => $picture)
{
$image_url = $picture['picture'];
$image_ext = pathinfo($image_url, PATHINFO_EXTENSION);
$imageData = base64_encode(file_get_contents($image_url));
$image = $this->_imageContentFactory->create()
->setType('image/jpeg')
->setName($sku . '-' . $key . '.' . $image_ext)
->setBase64EncodedData($imageData);

$entry = $this->_productAttributeMediaGalleryEntryFactory->create();
$entry->setContent($image);
$entry->setMediaType('image');
if (0 == $key)
$entry->setTypes(['image', 'small_image', 'thumbnail']);
else
$entry->setTypes(['image']);

$entry->setDisabled(false);
$entry->setPosition($key);

$entries = $entry;
}

$product->setMediaGalleryEntries($entries);

$product->setImage($entries[0]->getFile());
$product->setThumbnail($entries[0]->getFile());
$product->setSmallImage($entries[0]->getFile());
$this->_productRepository->save($product);


EDIT: after more investigation, it appears roles are only lost for 'all store views', in any other store views, the image rules are correctly detected.







magento2 import product-images thumbnail small-image






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 13 '17 at 17:22









Prince Patel

13.2k54674




13.2k54674










asked Oct 27 '16 at 11:17









Gael COAT

209113




209113





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • I am facing this exact issue! Only difference is we are using default Magento import
    – Hashid
    Apr 7 '18 at 6:56


















  • I am facing this exact issue! Only difference is we are using default Magento import
    – Hashid
    Apr 7 '18 at 6:56
















I am facing this exact issue! Only difference is we are using default Magento import
– Hashid
Apr 7 '18 at 6:56




I am facing this exact issue! Only difference is we are using default Magento import
– Hashid
Apr 7 '18 at 6:56










1 Answer
1






active

oldest

votes


















0














you need to used this to import product with images.



// Adding Image to product
$imagePath = $entries[0]->getFile(); // path of the image
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();





share|improve this answer





















  • Thanks for your help Dhiren, but I think this is not a good thing to use the almost deprecated $product->save() method, that's why i use repositories to save my product, and I never manage to got it work with addImageToMediaGallery(), i added the line where I save the product to the code snippet for info
    – Gael COAT
    Oct 28 '16 at 14:41













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f142966%2fimported-image-roles-are-lost-on-update-in-magento-2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














you need to used this to import product with images.



// Adding Image to product
$imagePath = $entries[0]->getFile(); // path of the image
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();





share|improve this answer





















  • Thanks for your help Dhiren, but I think this is not a good thing to use the almost deprecated $product->save() method, that's why i use repositories to save my product, and I never manage to got it work with addImageToMediaGallery(), i added the line where I save the product to the code snippet for info
    – Gael COAT
    Oct 28 '16 at 14:41


















0














you need to used this to import product with images.



// Adding Image to product
$imagePath = $entries[0]->getFile(); // path of the image
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();





share|improve this answer





















  • Thanks for your help Dhiren, but I think this is not a good thing to use the almost deprecated $product->save() method, that's why i use repositories to save my product, and I never manage to got it work with addImageToMediaGallery(), i added the line where I save the product to the code snippet for info
    – Gael COAT
    Oct 28 '16 at 14:41
















0












0








0






you need to used this to import product with images.



// Adding Image to product
$imagePath = $entries[0]->getFile(); // path of the image
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();





share|improve this answer












you need to used this to import product with images.



// Adding Image to product
$imagePath = $entries[0]->getFile(); // path of the image
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();






share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 28 '16 at 6:40









Dhiren Vasoya

4,23551642




4,23551642












  • Thanks for your help Dhiren, but I think this is not a good thing to use the almost deprecated $product->save() method, that's why i use repositories to save my product, and I never manage to got it work with addImageToMediaGallery(), i added the line where I save the product to the code snippet for info
    – Gael COAT
    Oct 28 '16 at 14:41




















  • Thanks for your help Dhiren, but I think this is not a good thing to use the almost deprecated $product->save() method, that's why i use repositories to save my product, and I never manage to got it work with addImageToMediaGallery(), i added the line where I save the product to the code snippet for info
    – Gael COAT
    Oct 28 '16 at 14:41


















Thanks for your help Dhiren, but I think this is not a good thing to use the almost deprecated $product->save() method, that's why i use repositories to save my product, and I never manage to got it work with addImageToMediaGallery(), i added the line where I save the product to the code snippet for info
– Gael COAT
Oct 28 '16 at 14:41






Thanks for your help Dhiren, but I think this is not a good thing to use the almost deprecated $product->save() method, that's why i use repositories to save my product, and I never manage to got it work with addImageToMediaGallery(), i added the line where I save the product to the code snippet for info
– Gael COAT
Oct 28 '16 at 14:41




















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f142966%2fimported-image-roles-are-lost-on-update-in-magento-2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

An IMO inspired problem

Management

Investment