If you have migrated data from magento 1 to magento 2 and still if you cant see product image on detail page.
Please follow below script and run it o your magento root directory it will be assign image to the product.
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
try
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$importDir = __DIR__ . '/pub/media/catalog/product';
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollection->load();
foreach($productCollection as $_product){
$id = $_product->getEntityId();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($id);
if($product->getImage() != '')
{
$url = $importDir . $product->getImage();
$product->addImageToMediaGallery($url, array('image', 'small_image', 'thumbnail'), true, false);
$product->save();
echo "<br /><br /> $id Product Save Succefully";
}
}
}
catch(\Exception $e)
{
echo $e->getMessage();
exit;
}
?>
Please follow below script and run it o your magento root directory it will be assign image to the product.
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
try
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$importDir = __DIR__ . '/pub/media/catalog/product';
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollection->load();
foreach($productCollection as $_product){
$id = $_product->getEntityId();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($id);
if($product->getImage() != '')
{
$url = $importDir . $product->getImage();
$product->addImageToMediaGallery($url, array('image', 'small_image', 'thumbnail'), true, false);
$product->save();
echo "<br /><br /> $id Product Save Succefully";
}
}
}
catch(\Exception $e)
{
echo $e->getMessage();
exit;
}
?>
Comments
Post a Comment
Feel Free To Ask...