Stocks786 Posted April 3, 2019 Share Posted April 3, 2019 Hi I'm really battling to find a way to create a PHP script to update my stock quantities in PS 1.6. What I need it to do is Upload CSV file that has my stock code (Reference) and quantities. Read that csv file and run an sql query to update the stock quantities. My CSV file has the following fields: Reference (Text as per stock code) Quantity (integer) I tried with some PHP Code and an SQL query but failed, miserably. I do know that my stock quantities is stored in the stock_available table and that the master products in the product table. Any assistance is appreciated. Thanks in advance. Link to comment Share on other sites More sharing options...
hakeryk2 Posted April 3, 2019 Share Posted April 3, 2019 Post this code that You used. Link to comment Share on other sites More sharing options...
Stocks786 Posted April 4, 2019 Author Share Posted April 4, 2019 This is the code that I've used... // -------------------------------------------------- // quantity_update.php // Change MySql database quantity and prices and other data from csv file // // To run open in your browser. // Example: http://www.mysite.com/secure_folder/update.php // // --------------------------------------------------- //Configuration variables //--------------------------------------------------- // Connect to MySQL change with your data mysql_connect("LOCALHOST", "root", "") or die(mysql_error()); mysql_select_db("presta100") or die(mysql_error()); // If first row of csv file is headings set $row to 1. $row = 0; // TABLE OF PRODUCTS AND VARIATIONS - IN PRESTASHOP IS PREFIX_ $update_table = "ps_product"; // Get the csv file - INSERT THE ADDRESS OF CSV FILE OR UPLOAD IT TO THE SOME FOLDER OF THIS SCRIPT $handle = fopen("update.csv", "r+"); // Go through the csv file and print each row with fields to the screen. // And import them into the database updating only the price and quantity // SET VARIABLES $reference, $price AND $quantity IF YOU ARE NOT UPDATING PRESTASHOP TABLES while (($data = fgetcsv($handle, 100000, ";")) !== FALSE) { $num = count($data); echo "\n"; echo "( $row )\n"; $row++; for ($c=0; $c < $num; $c++) { if ($c = 1) { $reference = $data[($c - 1)]; echo $reference . " - Reference Assigned\n"; } if ($c = 2) { $price = $data[($c - 1)]; mysql_query("UPDATE $update_table SET price='$price' WHERE reference='$reference'") or die(mysql_error()); echo $price . " - Price updated\n"; } if ($c = 3) { $quantity = $data[($c - 1)]; mysql_query("UPDATE $update_table SET quantity='$quantity' WHERE reference='$reference'") or die(mysql_error()); echo $quantity . " - Quantity updated\n"; } if ($c = 4) { $active = $data[($c - 1)]; mysql_query("UPDATE $update_table SET active='$active' WHERE reference='$reference'") or die(mysql_error()); echo $active . " - Activity updated\n"; echo "_____________________________________________________\n"; } } } fclose($handle); echo " \n"; echo " - - - SUCESSFULY COMPLETED - - - "; ?> Link to comment Share on other sites More sharing options...
hakeryk2 Posted April 4, 2019 Share Posted April 4, 2019 (edited) I don't see in code part when ps_stock_available is updated. You only update quantity column in ps_product table. query will be like this but check it first UPDATE ps_stock_available sa LEFT JOIN ps_product p ON (sa.id_product = p.id_product) SET sa.quantity = 'YOUR QUANTITY' WHERE p.reference = 'YOUR-REFERENCE-CODE' Remember to escape slash for $reference in code. You dont need to slash escape quantity value --------------------------------------------------------------------- Anyway You should try to use build in prestashop resource like require_once(dirname(__FILE__).'/../../config/config.inc.php'); // path to config // somekind of foreach or loop, while etc and put the code below into this $product = new Product(1234); // id of product if (Validate::isLoadedObject($product)){ $product->quantity = 5; // your quantity $product->save(); } Edited April 5, 2019 by hakeryk2 (see edit history) Link to comment Share on other sites More sharing options...
Stocks786 Posted May 2, 2019 Author Share Posted May 2, 2019 Thank you for your help. I will try this and get back to you. Link to comment Share on other sites More sharing options...
sinisa91 Posted November 28, 2019 Share Posted November 28, 2019 Am 2.5.2019 um 3:33 PM schrieb Stocks786: Thank you for your help. I will try this and get back to you. Hi can you send me the code who you push in your server ? I want Update Automatic my Stock and i find no code. Who you put the code? Thank You Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now