Magento 1.7 Images Not Importing

 

I just ran across a slight issue with the Magento Import that comes with 1.7. I had ran an export, tweaked it a bit, and then reran the import. I realized that while my products were getting imported, the images had disappeared from the site, but the system gave me no errors. After stepping the code, I found that it was two separate issues. The first was simple, my media/import folder didn’t exist. Once I created it, the system run better. However, an additional issue was happening.

It turns out that when you run the import, magento will only work with the files from the media/import. This means that if you do an export and the image is no longer in the media/import, you will loose the image in the DB, even though the file still exists on the server. Fortunately, it’s not to big of an issue to resolve, though it does require some code modifications.

I’m not going to worry about posting the code to create the module to rewrite the system properly and instead get to the code. The class you will need to rewrite is Mage_ImportExport_Model_Import_Entity_Product. The code you will replace is:

    /**
     * Uploading files into the "catalog/product" media folder.
     * Return a new file name if the same file is already exists.
     *
     * @param string $fileName
     * @return string
     */
    protected function _uploadMediaFiles($fileName)
    {
        try {
            $res = $this->_getUploader()->move($fileName);
            return $res['file'];
        } catch (Exception $e) {
            return '';
        }
    }

This is the easiest place to make your changes, as this method is always called to process the files. We are going to make two changes. The first is more of an OCD thing than anything, but if $fileName is blank, there is no fallback. That is annoying. So we will just throw in an if statment to kick us out if that is the case. Additionally, we will do a little reworking of the code so that if the exception occurs (which is what triggers when the file doesn’t exist in media/import) we can fall back a bit.

    /**
     * Uploading files into the "catalog/product" media folder.
     * Return a new file name if the same file is already exists.
     *
     * @param string $fileName
     * @return string
     */
    protected function _uploadMediaFiles($fileName) {
    	if (trim($fileName) == '') {
    		return '';
    	}
    	
    	$strReturn = '';
    	
        try {
            $res = $this->_getUploader()->move($fileName);
            $strReturn = $res['file'];
        } catch (Exception $e) {}
        
        if ($strReturn == '') {
        	if (file_exists($this->_getUploader()->getDestDir() . $fileName)) {
        		$strReturn = $fileName;
        	}
        }
        
        return $strReturn;
    }

As you can see, we keep the original code. All we do is add a fallback for when the file isn’t found to default back to what was sent through in the first place, if it exists.

 


If you found this article useful, please consider donating to show your support. Need help with something? Feel free to Drop-A-Line and we will try to assist where we can.

 
This entry was posted in Creations, Magento. Bookmark the permalink.