Jump to content
Stocks786

PHP Script for Updating stock quantities in PS 1.6

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.

Share this post


Link to post
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  -  -  -  ";
?>

Share this post


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

Share this post


Link to post
Share on other sites

Thank you for your help.

I will try this and get back to you.

 

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More