Make all store images the base, small and thumbnail images in Magento?
After making a change like that to the database, even if successful, you would need to rebuild the images cache in Cache Management.
You might be able to do it with a script like this and not worry about caching or indexing.
<?php
require 'app/Mage.php';
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach ($products as $product) {
if (!$product->hasImage()) continue;
if (!$product->hasSmallImage()) $product->setSmallImage($product->getImage());
if (!$product->hasThumbnail()) $product->setThumbnail($product->getImage());
$product->save();
}
?>Done!
Save this in your Magento directory and access it by typing the URL into your browser's address bar. I haven't tested this script yet.
Magento - Auto set base, small and thumbnail on upload
It is very easy. Example's here. Replace in root/js/mage/adminhtml/product.js
in the end of handleUploadComplete
around 120 row code
this.container.setHasChanges();
this.updateImages();
},
updateImages : function() {
with
this.container.setHasChanges();
this.updateImages();
$$('tr#media_gallery_content-image-1 td.cell-image.a-center input')[0].setAttribute('checked','checked');
$$('tr#media_gallery_content-image-1 td.cell-small_image.a-center input')[0].setAttribute('checked','checked');
$$('tr#media_gallery_content-image-1 td.cell-thumbnail.a-center input')[0].setAttribute('checked','checked');
},
updateImages : function() {
And enjoy autoselect first image after upload
Multi-store: Product image (default) import, base, small, thumbnail images are not setting in (non-default) store
Please confirm your OS, like Windows or Ubuntu?
If uploading CSV from Ubuntu, then please re-import via Windows.
Sometimes, import makes issue due to the OS.
Thanks,
magento 2 how to set base or thumbnail image to product
after loading the product:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load(PRODUCT ID);
You can get all the images of a product with the following code:
$images = $product->getMediaGalleryImages();
This will return a collection with all the images added to the product.
You can pick any image or take the first one like this:
$image = $images->getFirstItem();
After that you van get the image url with
$imageUrl = $image->getFile();
To set it to the product is this:
$product->setImage($image);
$product->setSmallImage($image);
$product->setThumbnail($image);
$product->setSwatchImage($image);
And save the product like this:
$product->save();
Magento - auto set base image, small image and thumbnail on import (in admin panel)?
I found my mistake, the issue was :
if($is_default==true){
$mediaAttribute = array (
'image'=>$image,
'small_image'=>$small_image,
'thumbnail'=>$thumbnail,
);
}else{
$mediaAttribute = null;
}
Now it works with :
$url = $image;
$image_type = substr(strrchr($url,"."),1); //find the image extension
$filename = $sku.'.'.$image_type; //give a new name, you can modify as per your requirement
$filepath = Mage::getBaseDir('media') . DS . 'import'. DS . $filename; //path for temp storage folder: ./media/import/
file_put_contents($filepath, file_get_contents(trim($url))); //store the image from external url to the temp storage folder
$mediaAttribute = array ( 'image',
'small_image',
'thumbnail',
);
$prod->addImageToMediaGallery($filepath, $mediaAttribute, true, false);
$prod->save();
Adding Magento's small_image and thumbnail Programmatically
Try with:
$product->addImageToMediaGallery('/tmp/test.png',array('image', 'small_image', 'thumbnail'),false,false);
Magento: How to programatically set the base image to the first image in the list
$productId = 1;
//load the product
$product = Mage::getModel('catalog/product')->load($productId);
//get all images
$mediaGallery = $product->getMediaGallery();
//if there are images
if (isset($mediaGallery['images'])){
//loop through the images
foreach ($mediaGallery['images'] as $image){
//set the first image as the base image
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('image'=>$image['file']), 0);
//stop
break;
}
}
Related Topics
SQL Server: How to Get All Child Records Given a Parent Id in a Self Referencing Table
What's the Most Efficient Way to Check If a Record Exists in Oracle
How to Insert Multiple Rows into Oracle with a Sequence Value
SQL Return Only Duplicate Rows
Keep Only N Last Records in SQLite Database, Sorted by Date
Custom Post Type Yearly/ Monthly Archive
Best Way to Get the Next Id Number Without "Identity"
Using MySQL in Clause as All Inclusive (And Instead of Or)
How to Use Limit Keyword in SQL Server 2005
The Best Way to Use a Db Table as a Job Queue (A.K.A Batch Queue or Message Queue)
Multiple Full Outer Join on Multiple Tables
Insert a Blob via a SQL Script
Group Data by the Change of Grouping Column Value in Order
Boolean 'Not' in T-SQL Not Working on 'Bit' Datatype
How to Gracefully Include Formatted SQL Strings in an R Script
How to Deal with M..N Relationships in a Relational Database