Changing Character Set for CSV Exports in Magento 1
If you’re facing issues with Spanish characters being corrupted or replaced during CSV exports from your Magento 1 store running version 1.7.0, chances are the character set used during export is not UTF-8. By default, Magento sets the charset to “Western Europe” for CSV files, which doesn’t support extended Latin characters.
Understanding the Issue
The problem arises because your store is configured to use Spanish (es_ES.UTF-8) as the locale and the database character set is set to utf8_general_ci. However, during the CSV export process in Magento 1.7.0, it defaults to a charset that doesn’t support UTF-8 encoded characters.
Identifying the Culprit
To confirm this is indeed the issue, you can check the content of the exported CSV file. You should see Spanish characters replaced with question marks or other non-Latin characters. This indicates that the export process is not handling Unicode characters correctly.
Changing the Character Set for CSV Exports
To resolve this problem, you need to modify two files in your Magento installation:
- lib/Zend/Csv.php
- app/code/core/Mage/Adminhtml/Block/Export/Csv.php
Modifying lib/Zend/Csv.php
In the Zend_Csv class of file lib/Zend/Csv.php, find the following line around line 61:
$csvFile = fopen($fileName, 'w+');
Replace it with this code snippet:
$csvFile = fopen($fileName, 'w+') or die("Could not open file"); // Add an error handling
fputs($csvFile, $bom . $enclosure); // Add BOM and enclosure character
fputs($csvFile, $delimiter, strlen($delimiter)); // Add delimiter
Here’s what this code does:
fopenopens the CSV file with write permissions.$bom = "\xEF\xBB\xBF";is inserted to ensure proper UTF-8 encoding.$enclosure = '""';sets the enclosure character (the double quotes).$delimiter = ',';maintains the default delimiter as a comma.
Modifying app/code/core/Mage/Adminhtml/Block/Export/Csv.php
Next, open app/code/core/Mage/Adminhtml/Block/Export/Csv.php. In the _prepareFile() method (around line 130), replace this section:
$file = fopen($filename, 'w');
With this updated code:
$file = fopen($filename, 'w+') or die("Could not open file");
fputs($file, $bom . $enclosure); // Add BOM and enclosure character
fputs($file, $delimiter, strlen($delimiter)); // Add delimiter
The changes in this file are similar to the previous one. It ensures that the CSV file is opened with proper permissions and that the header contains the necessary UTF-8 encoding information.
Save and Test Your Changes
After making these modifications, save both files and clear your Magento store’s cache.
To test if the changes were successful:
- Go to System > Import/Export > Data Flow.
- Select CSV as the export method.
- Choose an entity (e.g., Sales Orders).
- Click on Export.
Check the exported CSV file. It should now contain Spanish characters correctly encoded in UTF-8.
Additional Considerations
It’s important to note that changing core Magento files can potentially break functionality if updates are applied later. To avoid this, consider creating a custom module that overrides these two classes using the Magento 1 override method (app/etc/modules/Your_Module.xml and app/code/local/Your_Module/Zend/Csv.php).
If you need further assistance with Magento development or have other e-commerce platform questions, feel free to explore our services at Laravel Company. We specialize in Laravel application development and can help optimize your e-commerce workflows.