cancel
Showing results for 
Search instead for 
Did you mean: 

Product save is showing error script tried to execute a method or access a property of an incomplete

Product save is showing error script tried to execute a method or access a property of an incomplete

Hi Guys,

 

I have created a external script in magento version 2.4.2 for upload product image it was working perfect in my old domain location but after change new domain and location it is showing below error:

 

Fatal error: Magento\Framework\Message\Manager::addMessage(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Magento\Framework\Message\Collection" of the object you are trying to operate on was loaded before unserialize() gets called or provide an autoloader to load the class definition in /vendor/magento/framework/Message/Manager.php on line 146

 

<?php
include 'common.php';
if (!isset($_SESSION['DIID']))
{
    $f->redirect(APP_URL);
    exit();
}
use Magento\Framework\App\Bootstrap;
require __DIR__ . '../../../app/bootstrap.php';
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/import-new-laptop-images.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$directory = $_SERVER["DOCUMENT_ROOT"] . '/pub/media/uploads/';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
$productRepository = $obj->get('Magento\Catalog\Api\ProductRepositoryInterface');
$imageProcessor = $obj->create('\Magento\Catalog\Model\Product\Gallery\Processor');
$d = $db->get_row("select i_datetime from dimportlog where activity='Scan image laptop' order by dilid desc limit 0,1");
$sql = "SELECT c.sku FROM catalog_product_entity c left join catalog_product_website w on (c.entity_id=w.product_id)  where w.website_id=1 and (c.created_at >= '" . $d['i_datetime'] . "' or c.updated_at >= '" . $d['i_datetime'] . "') GROUP BY c.sku ORDER BY c.sku asc  ";
//$logger->debug('sql = ' . $sql .  '<br>');
$Query = $db->get_results($sql);
$numrows = $db->num_rows($sql);
if ($numrows > 0)
{
    foreach ($Query as $get)
    {
        try
        {
            $sku = $get['sku'];
            $product = $productRepository->get($sku);
            // Adding Image to product
            if ($product)
            {
                $imagePath = $directory . $sku . ".jpg"; // path of the image
                if (file_exists($imagePath))
                {
                    $eximages = $product->getMediaGalleryImages();
                    if (count($eximages) > 0)
                    {
                        foreach ($eximages as $child)
                        {
                            $imageProcessor->removeImage($product, $child->getFile());
                            $delpath = $_SERVER["DOCUMENT_ROOT"] . "/pub/media/catalog/product" . $child->getFile();
                            if (file_exists($delpath))
                            {
                                unlink($delpath);
                            }
                        }
                    }
                    $product->addImageToMediaGallery($imagePath, array(
                        'image',
                        'small_image',
                        'thumbnail'
                    ) , false, false);
                    $product->save(); // now this line is showing error
                    $logger->debug('Save Image for SKU ' . $sku . ' <br>');
                }
            }
        }
        catch(\Magento\Framework\Exception\NoSuchEntityException $e)
        {
            $product = false;
        }
    }
}
?>