Jump to content

update stock from .csv file


dostoyevski

Recommended Posts

Hello, I want to add products to the stock but of products that are already in the store, that is, what I want is to update the stock of products that are already in the store. For this I have the .csv with the data of the products and the quantities that are going to be added. But if I import them, it reloads the products as they are in the csv, but what I want is for it to increase the units of the products that are already added. how can i get that?

a greeting.

Link to comment
Share on other sites

Sample for pairing by product reference:


    include_once('./config/config.inc.php');

    // csv header
    // product_reference, product_quantity
    $csvFile = './update.csv';
    $csvSeparator = ',';
    $fieldProductReference = 0; // field product_reference 
    $fieldProductQuantity = 1; // field product_quantity

    $log = '';
    $log .= 'START IMPORT<br>';

    $db = Db::getInstance();

    $rowNum = 0;

    if (($handle = fopen($csvFile, "r")) !== false) {
        while (($data = fgetcsv($handle, 1000, $csvSeparator)) !== false) {
            $rowNum++;
            if ($rowNum == 1) {
                // SKIP FIRST LINE (HEADER)
                continue;
            }
            $reference = trim($data[$fieldProductReference]);
            $quantity = trim($data[$fieldProductQuantity]);
            $productAttributeId = 0;

            $getIdProduct = 0;

            if ($reference && is_numeric($quantity) && $quantity){
                // find ID product by reference
                $getIdProduct = $db->getValue("SELECT id_product FROM "._DB_PREFIX_."product WHERE reference = '".$reference."'");

                // product not found
                if (!$getIdProduct){
                    $log .= 'reference '.$reference.' not found <br>';
                    $rowNum++;    
                } else { 
                    // product found, update
                    StockAvailable::setQuantity($getIdProduct, $productAttributeId, $quantity);
                    $log .= 'product  '.$getIdProduct.' updated quantity '.$quantity.' <br>';
                }
            }

        } // END while

        $log .= 'END IMPORT<br>';
        echo $log;
    } // end foreach

 

Edited by 4you.software (see edit history)
Link to comment
Share on other sites

You don't have to create a module, just save the code in the root of the eshop and insert your CSV file in the same location.

If you put the file in another location, you need to change the path to config.inc.php and $csvFile.

Of course, from a security point of view, you can add your own token and read from the url using the $_GET method.

Edited by 4you.software (see edit history)
Link to comment
Share on other sites

E.g.:

$securityToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9';
$urlToken = $_GET['token'];

/* your url = https://my-website.com/importCsvFile.php?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9 */

/* check security token */
if ($securityToken != $urlToken){exit('token is not valid');}

/* and check if CSV file exists */
if (!file_exists($csvFile)){exit('csv file not found');}

 

Link to comment
Share on other sites

21 hours ago, 4you.software said:

You don't have to create a module, just save the code in the root of the eshop and insert your CSV file in the same location.

If you put the file in another location, you need to change the path to config.inc.php and $csvFile.

Of course, from a security point of view, you can add your own token and read from the url using the $_GET method.

Do I have to save it to a file and save it with the .php extension? what name do I give the file?

Link to comment
Share on other sites

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...