I'd like to share small thing with you. Its CSV stock update based on reference. It builds 2 arrays, one with values from CSV file and the other with values from database. Then loop compares reference's from csv with reference from DB. After that it either update quantity or just go to next reference if quantity is the same in both arrays. You can easly run it with cron and it works on ps 1.7.5. Also it creates logs with products that were updated.
I'm not good with php so feel free to improve it and share with others.
updateCSV.php
<?php set_time_limit(1800); #stock updater $database = require_once('../app/config/parameters.php'); $prefix = 'ps_'; $fileUrl = '';//file url $manufacturer_id = 0;//manufacturer id from prestashop $stockKey = 'Reference'; //file column reference header $stockQty = 'Quantity'; //file column quantity header // Create connection function connect(){ global $database; try{ $db = new PDO('mysql:host='.$database['parameters']['database_host'].';dbname='.$database['parameters']['database_name'], $database['parameters']['database_user'], $database['parameters']['database_password']); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); echo 'connected!.<br />'; }catch(PDOException $e){ echo 'connection failed.<br />'; } return $db; } function shop($prefix,$manufacturer_id){ $OnlyManufacturerProducts = ';'; if($manufacturer_id){ $OnlyManufacturerProducts = 'Where '.$prefix.'product.id_manufacturer ='.$manufacturer_id.';'; } $db = connect(); $array=array();//single $array2=array();//variant/combination $query= $db->prepare('SELECT '.$prefix.'product.active AS Active, '.$prefix.'product.id_manufacturer AS manuid,'.$prefix.'product_attribute.reference AS REF,'.$prefix.'product_attribute.upc AS UPC, '.$prefix.'stock_available.out_of_stock AS OOS,'.$prefix.'stock_available.quantity AS QTY, '.$prefix.'stock_available.location AS Location, '.$prefix.'product_attribute.available_date AS DeliveryDate from '.$prefix.'product LEFT JOIN '.$prefix.'product_attribute on '.$prefix.'product_attribute.id_product='.$prefix.'product.id_product LEFT JOIN '.$prefix.'stock_available on '.$prefix.'stock_available.id_product_attribute='.$prefix.'product_attribute.id_product_attribute '.$OnlyManufacturerProducts); try{ $query->execute(); $array2 = $query->fetchAll(PDO::FETCH_ASSOC); echo 'array filled.<br />'; }catch(PDOException $e){ echo 'array error.<br />'; } $query = $db->prepare('SELECT '.$prefix.'product.active AS Active, '.$prefix.'product.reference AS REF, '.$prefix.'product.upc AS UPC, '.$prefix.'stock_available.out_of_stock AS OOS, '.$prefix.'product.id_manufacturer AS manuid,'.$prefix.'stock_available.id_product_attribute AS BaseP, '.$prefix.'stock_available.quantity AS QTY, '.$prefix.'stock_available.location AS Location, '.$prefix.'product.available_date AS DeliveryDate FROM '.$prefix.'product LEFT JOIN '.$prefix.'stock_available ON '.$prefix.'stock_available.id_product='.$prefix.'product.id_product '.$OnlyManufacturerProducts); try{ $query->execute(); $array = $query->fetchAll(PDO::FETCH_ASSOC); echo 'array filled.<br />'; }catch(PDOException $e){ echo 'array error.<br />'; } //remove duplicates foreach($array as $f=>$val){ foreach($array2 as $f1=>$val2){ if($val['REF'] == $val2['REF']){ unset($array[$f]); } } } $stock = array_merge($array,$array2); return $stock; } Function readCSV($csvFile,$separator = ";"){ $array = $fields = array(); $i = 0; $handle = @fopen( $csvFile, "r"); if ($handle) { while (($row = fgetcsv($handle, 4096, $separator)) !== false) { if (empty($fields)) { $fields = $row; continue; } foreach ($row as $k=>$value) { $array[$i][$fields[$k]] = $value; } $i++; } if (!feof($handle)) { echo "Error: unexpected fgets() fail\n"; } fclose($handle); } return $array; } Function Update($csvFile,$prefix,$manufacturer_id,$stockKey,$stockQty){ $db = connect(); $csv = readCSV($csvFile); $shop = shop($prefix,$manufacturer_id); $product = $db->prepare('UPDATE '.$prefix.'product LEFT JOIN '.$prefix.'stock_available on '.$prefix.'stock_available.id_product='.$prefix.'product.id_product SET '.$prefix.'stock_available.quantity = :avbl WHERE '.$prefix.'product.reference = :indeks AND '.$prefix.'stock_available.id_product_attribute = 0'); $combination = $db->prepare('UPDATE '.$prefix.'product_attribute LEFT JOIN '.$prefix.'stock_available on '.$prefix.'stock_available.id_product_attribute='.$prefix.'product_attribute.id_product_attribute LEFT JOIN '.$prefix.'product on '.$prefix.'product_attribute.id_product='.$prefix.'product.id_product SET '.$prefix.'stock_available.quantity = :avbl WHERE '.$prefix.'product_attribute.reference = :indeks'); foreach($shop as $s){ $found = FALSE; $indeks = $s['REF']; $qty = $s['QTY']; $base = $s['BaseP']; if(!empty($indeks)){ foreach($csv as $i=>$stock){ $index = $stock[$stockKey]; $avbl = $stock[$stockQty]; if($indeks === $index){ $found = TRUE; if($qty != $avbl){ if(isset($base)){ try{ $product->execute([':avbl'=>$avbl,':indeks'=>$indeks]); $log = $indeks.PHP_EOL. "Single - Quantity updated from ".$qty." to ".$avbl.PHP_EOL. "-------------------------".PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); }catch(PDOException $e){ $log = $indeks.PHP_EOL. "error ".$e->getMessage().PHP_EOL. "-------------------------".PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); die(); } }else{ try{ $combination->execute([':avbl'=>$avbl,':indeks'=>$indeks]); $log = $indeks.PHP_EOL. "Variant - Quantity updated from ".$qty." to ".$avbl.PHP_EOL. "-------------------------".PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); }catch(PDOException $e){ $log = $indeks.PHP_EOL. "error ".$e->getMessage().PHP_EOL. "-------------------------".PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); die(); } } } } } } } } Function oldfile(){ $files = glob('./log/*.log'); $now = time(); foreach ($files as $file) { if (is_file($file)) { if ($now - filemtime($file) >= 60 * 60 * 24 * 3) { // 2 days unlink($file); } } } } $log = "⇊ Update Starts ⇊ ".date("H:i:s").PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); Update($fileUrl,$prefix,$manufacturer_id,$stockKey,$stockQty); $log = "⇈ Update Finished ⇈ ".date("H:i:s").PHP_EOL; file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); oldfile(); unset($database); exit("exit"); ?>