Jump to content

Importing special prices script


Recommended Posts

For anyone interested, here's a super simple script I created to be ale to import specific prices from csv, as I didn't find any other way to do this.

 

NOTE:

This is definitely a hack and not a proper way to extend Prestashop! The script as is has bunch of security issues, like no authentification, close to no validation etc.

 

However, it works well for me (I enabled access to the file from my IP only and I'm the one who's creating the csv and doing the import). I created this script to make my life easier with handling specific prices, and it made it...:-)

 

USE AT YOUR OWN RISK

 

NOTE 2:

The csv fields in this example are be product_id, group, percentage_discount, value_discount

Either percentage_discount or value_discount should be filled

 

NOTE 3:

The script can be easily extended to handle more fields, anyone with basic PHP skills will know how to do that. It is also easy to add more fields into the form to have some settings, like csv separator, setting csv columns -> db columns combos etc. 

<?php 

$csv = array();

// check that there are no errors
if($_FILES['csv']['error'] == 0){
    $name = $_FILES['csv']['name'];
    $ext = strtolower(end(explode('.', $_FILES['csv']['name'])));
    $type = $_FILES['csv']['type'];
    $tmpName = $_FILES['csv']['tmp_name'];

    // check the file is a csv
    if($ext === 'csv'){
        if(($handle = fopen($tmpName, 'r')) !== FALSE) {
            // I'm using meekrodb - http://www.meekro.com/ - normally, Prestashop framework should be used to access the db, but I have no time to learn it now..:-)
            require_once 'meekrodb.php';
            DB::$user = 'dbuser';
            DB::$password = 'dbpass';
            DB::$dbName = 'dbname';
            DB::$host = 'dbhost';
       
            // delete all specific prices, to reimport them later
            DB::delete('ps_specific_price','id_specific_price != 0');
          
            // necessary if a large csv file
            set_time_limit(0);

            $row = 0;

            while(($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
                // number of fields in the csv
                $num = count($data);

                // get the values from the csv
                $product_id = $data[0];
                $group = $data[1];
                $reduction_percent = $data[2];
                $reduction_amount = $data[3];
                
                // get reduction - either percent or amount
                $reduction = $reduction_percent != '' ? $reduction_percent : $reduction_amount;
                
                if ($reduction_percent != '') $reduction_type = 'percentage';
                if ($reduction_amount != '') $reduction_type = 'amount';
                
                // if group = 0 (Everybody), delete all other discounts, as the discounts in PS are cumulative, and I don't want to give my customers 120% discount...
                if ($group == 0) {
                  DB::delete('ps_specific_price', "id_product=%s", $product_id);
                }
                
                // check, if there's some discount with group = 0, if so, don't import, for the same reason as above
                $discount = DB::queryFirstRow("SELECT * FROM ps_specific_price WHERE id_product=%s AND id_group = 0", $product_id);
                if (is_null($discount)) {
                  DB::insert('ps_specific_price', array(
                    'id_specific_price_rule' => 0,
                    'id_cart' => 0,
                    'id_product' => $product_id,
                    'id_shop' => 0,
                    'id_shop_group' => 0,
                    'id_currency' => 0,
                    'id_country' => 0,
                    'id_group' => $group,
                    'id_customer' => 0,
                    'id_product_attribute' => 0,
                    'price' => '-1.000000',
                    'from_quantity' => 1,
                    'reduction' => $reduction,
                    'reduction_type' => $reduction_type,
                    'id_country' => 0,
                    'from' => '0000-00-00 00:00:00',
                    'to' => '0000-00-00 00:00:00'                                      
                  ));
                }

                // inc the row
                $row++;
            
            
            }
            fclose($handle);
            echo 'CSV was imported sucessfully';
        }
    }
}

?>

<html>
<body>

<form action="" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="csv" id="csv"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>
  • Like 3
Link to comment
Share on other sites

  • 7 months later...
  • 1 month later...
×
×
  • Create New...