Skip to content

Instantly share code, notes, and snippets.

@uabassguy
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uabassguy/e61f474d5e9749df5475 to your computer and use it in GitHub Desktop.
Save uabassguy/e61f474d5e9749df5475 to your computer and use it in GitHub Desktop.
<?php
$file = fopen('csv/import.csv','r');
$data = fgetcsv($file, null, ",");
$fields = $data;
$i = 0;
$j = 0;
while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
foreach($data as $value){
$f = $fields[$i];
$parsed[$j][$f] = $value;
$i++;
}
$j++;
$i = 0;
}
fclose($file);
//die(print_r($parsed));
require_once '../app/Mage.php';
Mage::app();
$storeId = 1;
$idsByPrice = array();
foreach ($parsed as $item) {
$id = Mage::getSingleton('catalog/product')->getIdBySku($item['sku']);
if ($id) { //if the product exists
$price = $item['price'];
if (!isset($idsByPrice[$price])) {
$idsByPrice[$price] = array();
}
//group all skus by price so you will have less updates.
$idsByPrice[$price][] = $id;
}
}
//now you have an array of product ids grouped by price
//$idsByPrice = array(
// '12.99' => array(2,3,7,12),
// '15.00' => array(9,44,22),...
//)
//die(print_r($idsByPrice));
foreach ($idsByPrice as $price=>$ids) {
Mage::getSingleton('catalog/product_action')->updateAttributes(
$ids, //ids to update
array('price'=>$price), //attributes to update
$storeId //store view to update the attribtues
);
}
@uabassguy
Copy link
Author

Setup:
place file in shell/
create csv folder in shell,
place import.csv in csv folder.
set price scope in magento to 'website' in Configuration > Catalog > Catalog > Price

Based on SO answer: http://magento.stackexchange.com/questions/19322/update-price-attribute-and-save-attribute/19330?noredirect=1#19330

@adriancr
Copy link

Hello, are you able to set different prices at Store View Level (not Store Level)? I can do it at the Website Level, but the price also gets updated in ALL the Store Views of the same website. I ask because I noticed your comments at SO, and it seems to me that you were able to set the price at Store View Level.

@uabassguy
Copy link
Author

This should do it at the store level you just need to set $storeId to match the desired store. 1 would be the default which affects all scopes which inherit it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment