Last active
August 29, 2015 14:23
-
-
Save jam13/c63ac57392bee2d5f248 to your computer and use it in GitHub Desktop.
Magento update skus + add barcodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'abstract.php'; | |
class Mage_Shell_Updateskus extends Mage_Shell_Abstract | |
{ | |
/** | |
* Run script | |
* | |
*/ | |
public function run() | |
{ | |
if (isset($this->_args['import'])) { | |
echo "Importing skus..."; | |
echo "done\n"; | |
// Need to use admin store to support disabled products | |
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); | |
$file = Mage::getBaseDir('var') . DS . "importexport" . DS . "updateskus.csv"; | |
$handle = fopen($file, 'r'); | |
if($handle) { | |
$record = array(); | |
while($record = fgetcsv($handle, 1000, ",")) { | |
$old_sku = $record[0]; | |
$barcode = $record[1]; | |
$new_sku = $record[2]; | |
echo "Updating ".$old_sku."..."; | |
try { | |
$get_item = Mage::getModel('catalog/product')->loadByAttribute('sku', $old_sku); | |
if ($get_item) { | |
$get_item->setOldsku($old_sku); | |
$get_item->setSku($new_sku); | |
$get_item->setBarcode($barcode); | |
$get_item->save(); | |
echo "done\n"; | |
} else { | |
echo "not found\n"; | |
} | |
} catch (Exception $e) { | |
echo "Cannot retrieve products from Magento: " . $e->getMessage()."\n"; | |
return; | |
} | |
} | |
fclose($handle); | |
} else { | |
echo "Cannot open " . $file ."\n"; | |
} | |
} else { | |
echo $this->usageHelp(); | |
} | |
} | |
/** | |
* Retrieve Usage Help Message | |
* | |
*/ | |
public function usageHelp() | |
{ | |
return <<<USAGE | |
Usage: php -f updateskus.php | |
import Import updated skus | |
help This help | |
USAGE; | |
} | |
} | |
$shell = new Mage_Shell_Updateskus(); | |
$shell->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment