Jump to content

Simple module: update order state via CSV upload


Beluga

Recommended Posts

Posting this here, because this is extremely simple in terms of functionality and would not perhaps warrant a proper release in the Free modules section. I welcome your feedback and you are free to post more sophisticated modifications and advancements!

 updateorderstate.zip

if (!defined('_PS_VERSION_'))
  exit;

class UpdateOrderState extends Module
{
    private $_html = '';
    public function __construct()
    {
        $this->name = 'updateorderstate';
        $this->tab = 'others';
        $this->version = '1.0';
        $this->author = 'Beluga';
        $this->need_instance = 0;
        
        parent::__construct();
        
        $this->displayName = $this->l('Update order state with CSV');
        $this->description = $this->l('Upload a CSV file to update the state of certain orders');
    }
    
    public function install()
    {
      if (parent::install() == false)
        return false;
      return true;
    }

    private function _displayForm() // the form must have enctype as multipart/form-data
    {
        $this->_html .=
        '<form action="'.Tools::htmlentitiesUTF8($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
            <fieldset>
            <legend>'.$this->l('Upload CSV').'</legend>                    
                    <input type="file" id="csv" name="csv" />
                    <input class="button" name="btnSubmit" value="'.$this->l('Upload').'" type="submit" />
                    <p>CSV structure: use , as delimiter. First field is the order id, second field is the state id.</p>
            </fieldset>
        </form>';
    }
    
    public function getContent()
    {
    $output = null;
 
        if (Tools::isSubmit('btnSubmit') && $_SERVER['REQUEST_METHOD'] == 'POST' && $_FILES['csv']['size'] > 0)
        {
            $file = $_FILES["csv"];
            $file_name = $file["name"];
            $ext = pathinfo($file_name, PATHINFO_EXTENSION);
            $ext = strtolower($ext);
            if ($ext != "csv") {
                $this->_html .= '<div class="alert error">Error: not a csv file.</div>';
            }
            else
            {
                $saveto_path_and_name = _PS_UPLOAD_DIR_.'file.csv'; // Where you want to save the file
                move_uploaded_file($file["tmp_name"], $saveto_path_and_name);
                if ($handle = @fopen($saveto_path_and_name, "r"))
                {
                    while (($data = fgetcsv($handle, 1000, ",")) !== false) // separator defined as comma
                    {                
                        $orderId = $data[0]; // first field is the order id
                        $orderState =  $data[1]; // second field is the state id
                        $history = new OrderHistory();
                        $history->id_order = (int)$orderId;
                        $history->changeIdOrderState((int)$orderState, $history->id_order);
                        $history->save();
                    }
                    fclose($handle);
                    unlink($saveto_path_and_name);                
                    $this->_html .= '<div class="conf confirm"> '.$this->l('Upload successful').'</div>';
                }
                else
                {
                    $this->_html .= '<div class="alert error">Error: could not open uploaded file!</div>';
                }
            }
        }
            $this->_displayForm();
            return $this->_html;
    }
}
Edited by Beluga (see edit history)
Link to comment
Share on other sites

  • 2 years later...
  • 5 years later...

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...