Jump to content

swap delimiter from ";" semicolon to "," comma on export


Heesang

Recommended Posts

Hi,

It's 1.6.1.15

I would like to receive my export CSV file delimited by comma "," instead of semicolon ";". 

Are there any way to by modify PHP code or DB entry?

When I import, I can choose what delimiter to use, but there is no option when export.

Thank you advance for any suggestion

  • Like 1
Link to comment
Share on other sites

This appears to be hardcoded in a template file in 2 places (one for the header row, and then one for all other rows).  You will see the semi-colon towards the end of each line.

\admin\themes\default\template\layout-export.tpl

{$export_precontent}{foreach from=$export_headers item=header}{$text_delimiter}{$header}{$text_delimiter};{/foreach}

{foreach from=$line item=content}{$text_delimiter}{$content}{$text_delimiter};{/foreach}

 

  • Thanks 1
Link to comment
Share on other sites

Thanks bellini13!

 

It wasn't working so I also changed

 

classes/CSV.php

public function __construct($collection, $filename, $delimiter = ';')
 

However it still won't work. still delimited by ";" when file exported even I cleared the cache.

 

Any idea?

 

Link to comment
Share on other sites

Hi bellini13

 

Yes, I have changed code on layout-export.tpl that ";" to "," however its still export ";" delimited to CSV

 

{$export_precontent}{foreach from=$export_headers item=header}{$text_delimiter}{$header}{$text_delimiter},{/foreach}

{foreach from=$export_content item=line}
{foreach from=$line item=content}{$text_delimiter}{$content}{$text_delimiter},{/foreach}

I have flushed cache but it still do same.

My template compile option is set on

 

Recompile templates if the files have been updated

And the cache is set ON

Do I have to do something else?

Thanks in advance!

Link to comment
Share on other sites

This is within the class ModuleGrid.  Since we are talking about Stats pages which is produced by a module (statsbestproducts), then we need to start with "statsbestproducts.php"

If we look inside statsbestproducts.php, we see a function named hookAdminStatsModules, with this code

        if (Tools::getValue('export'))
            $this->csvExport($engine_params);

 

This module does not have csvExport, so we look at the parent class ModuleGrid and see the csvExport being used below

    protected function csvExport($datas)
    {
        $this->_sort = $datas['defaultSortColumn'];
        $this->setLang(Context::getContext()->language->id);
        $this->getData();

        $layers = isset($datas['layers']) ?  $datas['layers'] : 1;

        if (isset($datas['option'])) {
            $this->setOption($datas['option'], $layers);
        }

        if (count($datas['columns'])) {
            foreach ($datas['columns'] as $column) {
                $this->_csv .= $column['header'].';';
            }
            $this->_csv = rtrim($this->_csv, ';')."\n";

            foreach ($this->_values as $value) {
                foreach ($datas['columns'] as $column) {
                    $this->_csv .= $value[$column['dataIndex']].';';
                }
                $this->_csv = rtrim($this->_csv, ';')."\n";
            }
        }
        $this->_displayCsv();
    }

And as you can see, semi-colon is once again hardcoded

So you can be tactical about this and define csvExport function in statsbestproducts.php and change the delimeter

Or you can change the Core ModuleGrid class directly (I wouldn't)

Or you can override the ModuleGrid class and create your own csvExport function so it affects all stat pages

Link to comment
Share on other sites

Thank you bellini13!

You are a life saver!

Now I have made a override file under

/override/classes/module

ModuleGrid.php

<?php

abstract class ModuleGridCoreOverride extends Module
{
    protected function csvExport($datas)
    {
        $this->_sort = $datas['defaultSortColumn'];
        $this->setLang(Context::getContext()->language->id);
        $this->getData();

        $layers = isset($datas['layers']) ?  $datas['layers'] : 1;

        if (isset($datas['option'])) {
            $this->setOption($datas['option'], $layers);
        }

        if (count($datas['columns'])) {
            foreach ($datas['columns'] as $column) {
                $this->_csv .= $column['header'].',';
            }
            $this->_csv = rtrim($this->_csv, ',')."\n";

            foreach ($this->_values as $value) {
                foreach ($datas['columns'] as $column) {
                    $this->_csv .= $value[$column['dataIndex']].',';
                }
                $this->_csv = rtrim($this->_csv, ',')."\n";
            }
        }
        $this->_displayCsv();
    }

}

 

I have set 

Disable all overrides

to NO
 
Clear the cache and removed cache/class_index.php
 
However the result was the same that it is still delimited with ";"
 
May I know what I did wrong?
 
Thank you in advance!
Link to comment
Share on other sites

This line does not look right

abstract class ModuleGridCoreOverride extends Module

I believe it should be this

abstract class ModuleGrid extends ModuleGridCore

 

You can also add a die statement in the first line of your function to confirm that you override is being executed

    protected function csvExport($datas)
    {
        die('my override file');

        $this->_sort = $datas['defaultSortColumn'];
        $this->setLang(Context::getContext()->language->id);
        $this->getData();
        ...
Link to comment
Share on other sites

  • 1 month later...

@Bellini13 I have a sort of similar problem. I have updated the code for the form.tpl file here: admin folder \themes\default\template\controllers\import\helpers\form\form.tpl to change the field separator and multiple value separator but they don’t change in the csv import section in the back office. The screenshot shows what I’m trying to do. 

Can you please tell me what other file must be edited?

Thank you.

1C0C6571-215B-49B7-98F6-CEC4260BF089.jpeg

Link to comment
Share on other sites

I was shown how to update my default CSV separators in the back office by a thirty bees member.

Edited this file:

/controllers/admin/AdminImportController.php


Line 568 Changed the comma to a caret (^)

Line 570 Changed the semicolon to a comma as follows
 
$this->separator = ($separator = Tools::substr(strval(trim(Tools::getValue('separator'))), 0, 1)) ? $separator : '^';
        $this->convert = false;
        $this->multiple_value_separator = ($separator = Tools::substr(strval(trim(Tools::getValue('multiple_value_separator'))), 0, 1)) ? $separator : ',';
    }

Edited by pause4paws
to add how to fix my issue (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...