Jump to content

PHP Script for Updating stock quantities in PS 1.6


Stocks786

Recommended Posts

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

  1. Upload CSV file that has my stock code (Reference) and quantities.
  2. 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

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

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 by hakeryk2 (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...
  • 6 months 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...