Jump to content

Edit History

ps8modules

ps8modules

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

 

ps8modules

ps8modules

Sample:


    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

 

×
×
  • Create New...