Updating Product Prices Using a .csv Programmatically

database-updateproduct-pricesprogrammaticallysoap

My question more broad at the beginning but I will go more into detail later.
I have around 3000 products where I have the Skus and prices in a .csv file. Now the prices changed recently and I would like to upload/update these prices of the products.
Is there a quick way to do this via a script using the SOAP API?

Or is there an easier way around this?

UPDATE
I have used this function http://www.blog.magepsycho.com/updating-product-prices-in-magento-in-easier-faster-way/ but now the value and price mismatch. Are there any better options going through a csv and then match the prices for each sku to a price.

Best Answer

I don't think that using the SOAP services will get you what you need fast. I mean it will probably work, but it will be really slow for 3k products.
Instead try this script.
Let's say that your csv has 2 columns. SKU and price.

$skuIndex = 0;
$priceIndex = 1;
$csv = 'path/to/file.csv';
$io = new Varien_Io_File();
//an array to keep the products by price
$productIdsByPrice = array();
//a product model instance
$productModel = Mage::getSingleton('catalog/product');
//read the csv
$io->streamOpen($csv, 'r+');
while($csvData = $io->streamReadCsv()){
   if (count($csvData) < 2) { //skip rows with less than 2 columns
       continue;
   }
   $sku = trim($csvData[$skuIndex]);
   //get the product id by sku
   $id = $productModel->getIdBySku($sku);
   if ($id) {
      //if the id exists then mark it for update
      $price = trim($csvData[$priceIndex]);
      if (!isset($productIdsByPrice[$price])) {
         $productIdsByPrice[$price] = array();
      }
      $productIdsByPrice[$price][] = $id;
   }
}
//now you should have the product ids you need to update grouped by price
//just do the fast update
foreach ($productIdsByPrice as $price => $ids) {
    Mage::getSingleton('catalog/product_action')->updateAttributes(
        $ids, //what products to update
        array('price' => $price), //what attributes to update
        0 //store id for update
    )
}

you can place this code in one of your custom models or helpers and then run it. You may want to double check the code. I haven't tested it. Also backup your db.

[EDIT]

If you run this from a file, create one called price_update.php on the same level as index.php and add this in it:

<?php 
error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd()); 
$mageFilename = MAGENTO_ROOT . '/app/Mage.php'; 
require_once $mageFilename; 
Mage::setIsDeveloperMode(true); 
ini_set('display_errors', 1);
umask(0);
Mage::app();

After this, add the code from above that parses the csv and call in your browser MAGE_ROOT/price_update.php.

Related Topic